Slimphp 3 Nested Group Not working - php

i have a little problem with slimphp router :
$app->group('/api', function () use ($app) {
// Library group
$this->group('/library', function () use ($app) {
// Get book with ID
$this->get('/books/:id', function ($req, $res) {
echo "books";
});
// Update book with ID
$this->put('/books/:id', function ($req, $res) {
});
// Delete book with ID
$this->delete('/books/:id', function ($req, $res) {
});
});
});
Sending A GET to /api/library/books/1 give me a Page not found Error, where is the problem.
EDIT :
.htaccess
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
PS: a simple app->get is working without any problem,

It is not found as Slim 3 uses {id} as placeholders, not :id as Slim 2 did. Therefore the code would be
$this->get('/books/{id}', function ($request, $response, $args) {
...
});
Found in Slim 3 Documentation for Route Placeholders

Related

Slim Framework routes returns not found except "get" named ones

I use: slim-skeleton (Mr. Rob Allen's scaffold provided on github)
Under /projects grouped routes, only "get" method routes work without any problem but with rest of all return page not found error. Also these routes have auth middleware + cors middleware (Cors Middleware taken from Slim Framework's v3 Documentation).
Here is my ../src/routes.php file:
use Slim\Http\Request;
use Slim\Http\Response;
// Routes
// Administration
$app->group('/admin', function() use ($app) {
// Dashboard
$app->get('/dashboard', 'App\Controllers\Admin\Dashboard:index');
// Projects and images management
$app->group('/projects', function() use ($app){
// Projects Actions
$app->get('/list', 'App\Controllers\Admin\ProjectManagement:index');
$app->post('/add', 'App\Controllers\Admin\ProjectManagement:add'); # NOT WORKING
$app->get('/id/{id}', 'App\Controllers\Admin\ProjectManagement:find');
$app->put('/edit/{id}', 'App\Controllers\Admin\ProjectManagement:edit'); # NOT WORKING
$app->delete('/remove/{id}', 'App\Controllers\Admin\ProjectManagement:remove'); # NOT WORKING
// Project Images Actions
$app->get('/{pid}/images', 'App\Controllers\Admin\ProjectImageManagement:attachments');
$app->post('/{pid}/images/attach', 'App\Controllers\Admin\ProjectImageManagement:attach');
// Project's Image management
$app->get('/{pid}/images/id/{id}', 'App\Controllers\Admin\ProjectImageManagement:find');
$app->put('/{pid}/images/edit/{id}', 'App\Controllers\Admin\ProjectImageManagement:edit');
$app->delete('/{pid}/images/remove/{id}', 'App\Controllers\Admin\ProjectImageManagement:removeImage');
/**
* Project's Image Sort Order
*
* Additional Info:
*
* GET /{pid}/images Retrieves current sort order beforehand
*
*/
$app->put('/{pid}/images/order/{id}/resort', 'App\Controllers\Admin\ProjectImageManagement:sortOrder');
});
// Page management
$app->group('/pages', function() use ($app) {
// About Page
$app->get('/about/content', 'App\Controllers\Admin\PageManagement:aboutPage');
$app->put('/about/content/update', 'App\Controllers\Admin\PageManagement:updateAbout');
// Contact Page
$app->get('/contact/content', 'App\Controllers\Admin\PageManagement:contactPage');
$app->put('/contact/content/update', 'App\Controllers\Admin\PageManagement:updateContact');
});
// Settings
$app->group('/settings', function() use ($app) {
// Account
$app->get('/account/details', 'App\Controllers\Admin\Settings:accountDetails');
$app->post('/account/details/apply', 'App\Controllers\Admin\Settings::applyAccountSettingChanges');
});
})->add($auth);
// Auth
$app->get('/auth/point', 'App\Controllers\AuthController:checkPoint');
$app->post('/auth/login','App\Controllers\AuthController:login');
$app->get('/auth/logout', 'App\Controllers\AuthController:logout');
// Guest
$app->get('/about', 'App\Controllers\Guest\PageContents:about');
$app->get('/contact', 'App\Controllers\Guest\PageContents:contact');
$app->get('/works', 'App\Controllers\Guest\ProjectExplorer:projects');
And here is my .htaccess under /public directory:
<IfModule mod_rewrite.c>
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# Determine the RewriteBase automatically and set it as environment v
ariable.
# If you are using Apache aliases to do mass virtual hosting or
installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
I also added Allow from all, but it results in same way. But if i have doing something wrong why all other routes created outside of "/projects" work well. For instance, Auth related routes work well.
I know that, I have to see "Method Not Allowed" warning instead of seeing not found. Where am i doing something wrong? Any help would be very appreciated.
EDIT:
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
$handler = $this->notFoundHandler; // handle using the default Slim page not found handler
return $handler($req, $res);
});
I forgot this one. This mapping hides/exchanges method not allowed page => not found handler. It seems to me as an application based error. Sorry for my carelessness...
My suggestion is to add a root for the group you added
$app->group('/admin', function(){};
Under this you should add
$this->get('', function(){/*-----activities----*/});
And try to use $this while using the groups
Explanation:
when you are using $app you are referring to slim object.
when you are using $this inside your $app->group(){} then you are referring to grouped object which will be responsible for grouping the statements.
So, basically when you use $app inside the group then you make it useless because your slim-app is looking for it on above layer of the code(ie. outside the group). and you are not using group object($this in this case) to declare routes so eventually they are getting declared but not addressed properly.
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
$handler = $this->notFoundHandler; // handle using the default Slim page not found handler
return $handler($req, $res);
});
I forgot this one. This mapping hides/exchanges method not allowed page => not found handler. It seems to me as an application based error. Sorry for my carelessness...
But in my opinion notFoundHandler is not the right choice for this. A custom error message has to be raised for this situation like 405 Method Not Allowed - (notFoundHandler returns 404)
Not sure if this is the case, but if you need to change $app->get or $app->post to $this->get or $this->post if you group the routes.

slim skeleton framework Post Method gives "Method not allowed"

I tried almost everything
but I keep receiving
Method not allowed
Method not allowed. Must be one of:
POST
The code that I'm using is:
$app->post('/ticket/new', function (Request $request, Response $response) {
// Create new book
echo 'a;';
});
I'm trying on postman
method: post
url: /public/ticket/new
header: Content-Type application/javascript
Any clue will help me
.htaccess is ok:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Thanks a lot
Have you tried something like:
$app->map(['GET', 'POST'],'/ticket/new', function (Request $request, Response $response) {
// Create new book
echo 'a;';
});

Lumen GET Requests Return 404 Error

define('ROUTE_BASE', 'lumen/public');
$app->get(ROUTE_BASE . '/', function () use ($app) {
return $app->welcome();
});
$app->get(ROUTE_BASE . '/test', function () use ($app) {
return 'test data : 123 abc !';
});
When I access 'localhost/lumen/public/' I can see the 'lumen welcome page'.
But if I try to access 'localhost/lumen/public/test', I receive the following error.
Error: its not found(404).
Laravel expects the public directory to be the webroot of your domain. As this is not true in your case, you will need to make some alterations to your .htaccess.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /lumen/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Also worth noting that instead of using a constant, you can use route groups to achieve the same functionality in your routes.php.
$app->group(['prefix' => 'lumen/public'], function ($app) {
$app->get('/', function () {
//welcome
});
$app->get('test', function () {
return 'test data : 123 abc !';
});
});
your lumen project must be put in webroot of your localhost,domain or virtual host not in subfolder of your webroot without edit your .htaccess.
for access your project in browser : http://lumen.laravel.dev not http://lumen.laravel.dev/public/
I hope this help. sorry for my English :)

htaccess config for slim php framework

I use Slim php framework to build some action inside index.php page:
so this is the way I call my action:
index.php/action1
$app->get('/action1',function () use ($app){
echo "this is action 1";
});
index.php/action2
$app->get('/action2',function () use ($app){
echo "this is action 2";
});
Now I want my url become pretty , such as when type in index/action1
, it will redirect to index.php/action1
Please provide me the solution in creating htaccess to do thatThank and best reagard
Create .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
user group to prefix your route
$app->group('/index', function () use ($app) {
/**
* Route GET /index/action1
*/
$app->get('/action1',function () use ($app){
echo "this is action 1";
});
});

url rewrite and google robots txt

I use php slim framework, with url rewrite here is my host real file structure:
like most framework all rewrite to index.php
/.htaccess
/assets
/index.php
/phpinfo.php
/robots.txt
/usr
/controller
/model
...
and here is my router
$app->get('/backstage', $authenticate(), function () use ($uri, $app) {...});
$app->get('/api', $authenticate(), function () use ($uri, $app) {...});
$app->get('/', function () use ($uri, $app) {...});
$app->get('/article', function () use ($uri, $app) {...});
How to disable /backstage, /api in my route, and real file path /phpin.php, /usr,
And accept /, /article in router?
I'm confusing should I fill router path or real file path? because real file path there is not exist /article
and this is I tried
User-agent: *
Disallow: /backstage/
Disallow: /phpinfo.php
First (assuming you use apache), you need to make sure your .htaccess file correctly points requests to your router file.
--- begin .htaccess snippet ---
<IfModule mod_rewrite.c>
RewriteEngine On
## direct all requests to Slim router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ slim-router.php [QSA,L]
</IfModule>
--- end .htaccess snippet ---
I may not be understanding your question properly, but to disable a route, simply comment out the line in slim-router.php that adds the route.
Adding entries to robots.txt will not prevent browsers from reaching a URI, it only exists to ask search engine bots (i.e., GoogleBot) to not index that particular URI. See robotstxt.org and the robots.txt entry on Wikipedia.
To direct a route to an existing file, you can use the \Slim\View class (see the \Slim\View documentation).
This example expects a file named templates/article.php to exist, which is the file that will output the content for the /article route.
Using the \Slim\View class, you can also send data to the template file, which I have demonstrated below as well. This is only a basic example, see the documentation for more complex usage.
//--- begin slim-router.php ---
$app = new \Slim\Slim();
$defview = new \Slim\View();
$defview->setTemplatesDirectory(realpath(__DIR__).'/templates');
$app->get(
'/article',
function () use ($app) {
global $defview;
//show the contents of 'templates/article.php', passing optional data to the template file:
$app->view($defview)->display(
'article.php',
array(
'data_one'=>'one',
'data_two'=>2,
'three'=>'3',
)
);
}
);
//--- end slim-router.php ---

Categories