We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Why I can't use post route in Micro Application

I follow the tutorial 3 and add di for models folder

This is my index.php (assume my project folder is api) `` <?php

// Use Loader() to autoload our model $loader = new \Phalcon\Loader ();

$loader->registerDirs ( array ( DIR . '/models/' ) )->register ();

$di = new \Phalcon\DI\FactoryDefault ();

// Set up the database service $di->set ( 'db', function () { return new \Phalcon\Db\Adapter\Pdo\Mysql ( array ( "host" => "localhost", "username" => "root", "password" => "root", "dbname" => "test" ) ); } );

// Create and bind the DI to the application $app = new \Phalcon\Mvc\Micro ( $di );

// This is the start route $app->get ( '/', function () { echo "<h1>Welcome!</h1>"; } );

$app->get ( '/say/welcome/{name}', function ($name) { echo "<h1>Welcome $name!</h1>"; } );

$app->post ( '/test', function () { echo "Test post route"; } );

$app->notFound ( function () use($app) { $app->response->setStatusCode ( 404, "Not Found" )->sendHeaders (); echo 'This is crazy, but this page was not found!'; } );

$app->handle (); ``

When I run localhost/api/test, it show "This is crazy, but this page was not found!" in the notFound handler. GET route is fine, but POST route is error. Why is that? Thank you!



42.1k

Hi. You are trying to run localhost/api/test A route you have set a '/test' Try run 'localhost/test'

Like I said, I assume that my project folder is "api", and index.php is inside "api" folder

GET route work fine, so I think my url is correct

Thank you for quickly answer, do you have any other ideas?