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;';
});
Related
I'm learning Slim Framework and got stuck with its routing.
Working Code. Code snippet #1:
$app = new \Slim\App();
$app->get("/", function () {
echo "Hello SlimFramework";
});
$app->run();
Not Working. Code snippet #2:
$app = new \Slim\App();
$app->get("/hello/{name}", function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
I'm getting "Not Found
The requested URL /hello/name was not found on this server." for Code snippet #2. Any clue what's going on here ?
.htaccess File
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Thanks in advance !
I had to start with clean slate in order to solve this issue for me. #ceejayoz's solution about making sure if .htaccess is working correctly, was a great help to test .htaccess configuration.
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
Is there a nice and supported way to define routes like this:
$app->get('/', function (Request $request, Response $response) {
});
$app->get('/hello/{name}', function (Request $request, Response $response) {
});
... and get them work as expected no matter the location of the index.php routing file?
E.g. if DOCUMENT_ROOT is C:\Projects\Playground (http://playground.example.com) and my router file is located at C:\Projects\Playground\PHP\Slim\foo\bar\index.php (http://playground.example.com/PHP/Slim/foo/bar) I'd like http://playground.example.com/PHP/Slim/foo/bar/hello/world! to match '/hello/{name}'.
(Every other file belonging to Slim would be somewhere else, let's say C:\Libraries\Slim, and should be irrelevant to this question.)
With nice and supported I mean not this:
$uri_prefix = complex_function_to_calculate_it($_SERVER['DOCUMENT_ROOT'], __DIR__);
$app->get($uri_prefix . '/hello/{name}', function (Request $request, Response $response) {
});
Slim 3 uses nikic/fast-route but I couldn't find any hint.
Besides creating .htaccess file in the subdirectory:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
you need to either configure web server or slim:
Solution 1 (configure apache):
On debian 9 open the following file:
/etc/apache2/apache2.conf
Once opened, modify
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
into
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
and then restart the apache to apply the changes:
systemctl restart apache2
Solution 2 (configure slim):
Attach this:
// Activating routes in a subfolder
$container['environment'] = function () {
$scriptName = $_SERVER['SCRIPT_NAME'];
$_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);
return new Slim\Http\Environment($_SERVER);
};
Note that you'll need to use subdir name in the routes, e.g. /subdir/ for home, or some specific route: /subdir/auth/signup
Oh well... If it isn't supported out of the box, I guess it's better to just not complicate it too much and make it a parameter:
define('ROUTE_PREFIX', '/PHP/Slim/foo/bar');
$app->get(ROUTE_PREFIX . '/hello/{name}', function (Request $request, Response $response) {
});
After all, writing generic code that calculates it dynamically under all circumstances is too much of a burden, esp. when you can have endless factors like symlinks or Apache aliases.
The parameter should of course be defined with the rest of your site settings (either src/settings.php if you use Slim Skeleton, .env if you use phpdotenv... whatever).
You could nest all of your routes inside of a wrapper group in Slim, for which the path pattern is determined based on the $_SERVER variable:
$app = Slim\Factory\AppFactory::create();
$app->group(dirname($_SERVER['SCRIPT_NAME']), function($subdirectory) {
$subdirectory->group('/api/v1', $function($api) {
// define routes as usual, e.g...
$api->get('/foo/{foo:[0-9]+}', function($req, $res) {
// ...
});
});
});
A little pre-processing of the $_SERVER['SCRIPT_NAME'] variable lets you do other things -- detect that your API is nested inside a directory named API and don't append another API to the route pattern, for example.
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 :)
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 ---