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

404 page not work

my routes

$router = new Phalcon\Mvc\Router();

$router->add('/login/', array(
    'controller'    => 'user',
    'action'        => 'login'
));

$router->add('/signup/', array(
    'controller'    => 'user',
    'action'        => 'signup'
));

$router->notFound(array(
    'controller'    => 'user',
    'action'        => 'show404'
));

return $router;

my controler

class UserController extends ControllerBase
{

    public function loginAction()
    {

    }

    public function signupAction()
    {

    }

    public function show404Action()
    {
        $this->response->setHeader(404, 'Not Found');
        $this->view->pick('404/404');
    }
}

test

site.com/login/ -> ok

site.com/signup/ -> ok

site.com/test/ -> TestController handler class cannot be loaded <- wtf?

how to make?

HI @stanleer ! Use $router = new Phalcon\Mvc\Router( FALSE ); for disable auto-router ( https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior )



25.0k

add false not work too

I can't see Routes file and Controller file... anyway i would check the Namespace of the TestController or if test is an action of the IndexController (and this is a default route) you should check the RewriteLog (enabling it in the .htaccess) as in rewriting can happen a lot of problem... (for example: https://forum.phalcon.io/discussion/1393/indexcontroller-cause-unmanaged-404-error-on-apache-w-mod-rewrit )



25.0k

first post corrected

Oh, sure! Your router contain default routings to :controller and :action parameters. (if you debug it you will find this default routes this in the $router object) try to insert $router->clear(); just before any $router->add...



25.0k
Accepted
answer

solution

routes.php

<?php

$router = new Phalcon\Mvc\Router(false);

// 404 страница
$router->notFound(array(
        'controller'    => 'error',
        'action'                => 'show404'
));

$router->add('/login/', array(
        'controller'    => 'user',
        'action'                => 'login'
));

$router->add('/signup/', array(
        'controller'    => 'user',
        'action'                => 'signup'
));

...

return $router;

ErrorController.php

<?php
use Phalcon\Mvc\View;

class ErrorController extends \Phalcon\Mvc\Controller
{

        public function show404Action()

                $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
                $this->response->setStatusCode(404, "Not Found");
        }
}