I am trying to build an api with SLIM Framework V4.
htdocs/slim/public/index.php
<?php
require __DIR__ . '/../vendor/autoload.php';
use DI\Container;
use Slim\Factory\AppFactory;
use Illuminate\Pagination\Paginator;
use App\Middlewares\RequestUser;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
// Create Container using PHP-DI
$container = new Container();
// Set container to create App with on AppFactory
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
$app->get('/', function (Request $request, Response $response, $args) {
$data = array('message' => 'Unauthorized access');
$payload = json_encode($data);
$response->getBody()->write($payload);
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
});
$app->run();
If i run php server, it works fine
G:\xampp\htdocs\slim\public> php -S localhost:8888
PHP 7.2.27 Development Server started at Sun Apr 19 18:42:11 2020
Listening on http://localhost:8888
Document root is G:\xampp\htdocs\slim\public
If i run
http://localhost/slim/public/
I am getting
Fatal error: Uncaught Slim\Exception\HttpMethodNotAllowedException: Method not allowed. Must be one of: OPTIONS in G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:96 Stack trace: #0 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\App.php(211): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 G:\xampp\htdocs\slim\vendor\slim\slim\Slim\App.php(195): Slim\App->handle(Object(Slim\Psr7\Request)) #4 G:\xampp\htdocs\slim\public\index.php(37): Slim\App->run() #5 {main} thrown in G:\xampp\htdocs\slim\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 96
htdocs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
I am working on windows 10, xampp v3.2.4
The production will be go daddy shared hosting, as a subfolder. I tested it as a subdomain on godaddy server it is working fine. But i need it to be a sub folder of main domain, as godaddy is charging SSL certificate for subdomain too.
Thanks for the help in advance.
Related
I am writing an API with Slim Framework but I've been getting 404 error
require ('Slim/Slim/Slim.php');
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->response()->header('Content-Type', 'application/json;charset=utf-8');
$app->get('/', 'test'); // It is OK and I can see the return of my function "test" when I access domain/api
$app->get('/groups', 'getGroups'); // 404 error while I try to go to domain/api/groups
Someone know how to fix this route problem ?
It looks like a htaccess issue. That htaccess must be placed in your api folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I am using a home grown small CGI Router for a simple webapp, code can be found here on github
The webapp has a login form like this
my $form = start_form( -method => 'post', -action => '/login' );
$form .= p( label('Mail'), textfield( -name => 'mail' ) );
$form .= p( label('Mail'), password_field( -name => 'pass' ) );
$form .= p( submit( -value => 'Log ind', -class => 'button button-primary' ) );
$form .= end_form();
and I have a router handling the post request like this
$router->add_route( 'POST', '/login', sub {
if ( param ) {
# Get mail and pass
my $mail = $cgi->param( 'mail' );
my $pass = $cgi->param( 'password' );
# Check against user table
# Set cookie and redirect to admin
}
# Otherwise redirect to /login
print redirect( -url => '/login' );
});
on my local environment, osx 10.10.3, perl 5, version 18, subversion 2 (v5.18.2), this is working like expected, I submit the form and handle the login, no problem.
my production environment is a Microsoft-IIS/5.0 according to ENV{'SERVER_SOFTWARE'}
On the production environment is returning a 404 and unfortunately I am unable to get my hands on any log file that could reveal something useful.
.htaccess file on both production and local environment
# RewriteBase /
DirectoryIndex index.pl index.phtml index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.pl [L,QSA]
The problem is most likely down to differing environments, check not just the server software but also the versions of perl and versions of the CGI.pm module. Also worth noting is that IIS can cause problems with CGI.pm due to the whole NPH thing - check the POD for CGI.pm for more details.
With regards to CGIRouter:
Out of the box CGI qw/:standard/ offers many of the features you would expect from a web framework, it has methods for creating HTML markup like anchors, paragraphs and H tags. CGI may not be as sexy as some of the modern frameworks.
It's not that CGI.pm isn't "sexy", it's simply not fit for purpose in any modern web app. Not to mention that the HTML markup functions are considered deprecated and shouldn't be used
adding a framework, any framework, would mean adding modules and dependencies which would then have to be kept up to date, as well as spending time on understanding the framework in question, it's cons and pros in order to pick the best one suited for the task at hand.
This is also a none-argument. CGI.pm has been removed from the perl core as of 5.22 so you will need to install it and its dependencies pretty soon. If you want to write a trivial RESTful web service using a simple to use framework with minimal dependencies then look at Mojolicious::Lite. For more examples and other alternatives see CGI::Alternatives.
After doing a ton of testing, I realised, the problem is not in the router or the fact that POST is not handled by IIS. The problem is the redirect subroutine.
Whereas redirect( -url => '/admin' ); works on OSX, it's not working on IIS. On IIS the full URL has to be passed like so redirect( -url => 'http://example.com/admin' ).
This is a follow up for this question Apache directory directive authentication based on Perl CGI::Session
Alias /files /myData/all
<Directory /myData/all >
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ cgi-bin/checklogin.pl?url=/files/$1 [L,R]
Options +Indexes
</Directory>
/files is a directory listing.
I edited the directive so that if a user goes to webserver/files/ they are redirected to checklogin.pl which checks for the existance of a session. If there is one it should redirect to the ?url if not it takes them to the loginpage.
The first part works. The redirect to $url causes a loop.
my $url = $cgi->param("url");
my $cookie = $cgi->cookie('mysession');
if(!$cookie){
$session = new CGI::Session();
print $session->redirect('/loginpage.html');
}else{
# HOW DO I display folder or files now?
$session = new CGI::Session(undef, $cookie, {Directory=>'/tmp/'});
print $session->redirect($url);
}
I am, obviously, getting a redirect loop error in Apache
This webpage has a redirect loop
If you redirect every request to checklogin.pl, any requests made inside checklogin.pl will redirect to checklogin.pl, which will redirect to checklogin.pl, which will redirect to checklogin.pl...
Instead, why don't you make a master script with a logged_in function that you call with each request:
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
sub logged_in {
# Check session
}
my $q = CGI->new;
if (not logged_in()) {
print $q->redirect('http://url/to/login.html'); # Need to use a full URL
}
else {
# Do stuff
}
I am trying to open URL of article from Facebook page on my website but the problem is every time error 404 is raised although the article is exists .
After searching I find that there is problem in .htaccess codes and I need help in this.
The problem :
URL : like from facebook page
Website : worked like Real link
As you can see there different between this two links.
The part is added "component/content/" and without the another language prefix "ar".
Some Details :
This is my .htaccess file :
> ##
# #package Joomla
# #copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
# #license GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
##
## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks
## Mod_rewrite in use.
RewriteEngine On
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
# Forward to sitemap
Redirect /sitemap.xml http://www.businesstendersmag.com/ar/?option=com_xmap&view=xml&tmpl=component&id=3
## End - Joomla! core SEF Section.
That's all! Any help will be appreciated.
How do you generate URLs that are posted to Facebook?
Are you generating URLs like this?
// require com_content router.
require_once (JPATH_SITE.'/components/com_content/helpers/route.php');
// generate link.
$link = JURI::root() . substr( JRoute::_( ContentHelperRoute::getArticleRoute( $article->slug, $article->categorySlug ) ), strlen(JURI::base(true)) + 1);
// Where $article->slug & $article->categorySlug are fetched from database.
I am new to Ez Publish, I would like to make the default Rest API working as a first step and next developing my own Rest API extension for mobile communication purpose but I am blocked at the first one.
when I try to get the result from "www.mydomain.com/api/ezp/content/node/2/list" I get "{"error_message":"Not Found"}"
configurations are done:
setting/override/site.ini.append.php
[ExtensionSettings]
ActiveExtensions[]
ActiveExtensions[]=ezprestapiprovider
...
extension/ezprestapiprovider/settings/rest.ini.append.php
<?php /* #?ini charset="utf-8"?
[ApiProvider]
ProviderClass[ezp]=ezpRestApiProvider
[Authentication]
RequireAuthentication=disabled
*/ ?>
.htaccess
DirectoryIndex index.php
RewriteEngine On
RewriteRule api index_rest.php [L]
RewriteRule ^index_rest\.php - [L]
...
I apologize for my English.
Edit1: Ez Publish version 4.5
Edit2: It seems to be a problem of RewirteRule, when I change "www.mydomain.com/api/ezp/content/node/2/list" to "www.mydomain.com/index_rest.php/api/ezp/content/node/2/list" it works , how can I fix that ?
Edit3:
I tried all rewriterules below, still not working except the last one.
# RewriteRule ^/api/ /index_rest.php [L]
# RewriteRule ^api/(.*) /index_rest.php/$1 [R=302,NC]
# RewriteRule ^/api/(.*) /index_rest.php/api/$1 [NC,L]
RewriteRule api index_rest.php [L]
But it seems to create conflit when I try to add new extension...
I added a new extension and when I try to access to it I got an error on the log file:
Unexpected error, the message was : The API provider 'mobile' could not be found. in mydomaine.com\kernel\private\rest\classes\rest_provider.php on line 37
Try replacing those rewriterules with the one specified by eZ
RewriteRule ^/api/ /index_rest\.php [L]
This is listed here for version 4.5: http://doc.ez.no/eZ-Publish/Technical-manual/4.5/Installation/Virtual-host-setup
I just regenerate autoload and it's working now. I use cmd line to do it (regenerating autoload with admin module does not work for me).