Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Custom Routing working for '/' but no others

I have a very simple implementation that follows the docs and some things I've read, but I'm still stuck. Displaying the '/' route works, but the other routes (/login, /masses, /vendors) do not work... I get a 404 for all of them. AJAX routes are also broken, but I'd like to solve the page routes issue first.

Here is the code:

/config/routes.php:

use Phalcon\Mvc\Router;

$router = new Router(false);
$router->removeExtraSlashes(true);

$router->addGet("/", [
    'controller' => 'noauth',
    'action'     => 'home',
]);

$router->addGet("/masses", [
    'controller' => 'noauth',
    'action'     => 'masses',
]);

$router->addGet("/vendors", [
    'controller' => 'noauth',
    'action'     => 'vendors',
]);

$router->addGet("/login", [
    'controller' => 'noauth',
    'action'     => 'login'
]);

return $router;

/app/controllers/NoauthController.php:

use Phalcon\Mvc\Controller;

class NoauthController extends Controller
{

    public function initialize() {
        $this->view->setTemplateAfter('layout');
    }

    public function homeAction() {
        $this->view->pick("noauth/home");
    }

    public function loginAction() {
        echo "login";
    }

    public function massesAction() {
        echo "masses";
    }    
    public function vendorsAction() {
        echo "vendors";
    }

}

/public/index.php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;

try {

    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        '../app/controllers/',
        '../app/models/'
    ))->register();

    // Create a DI
    $di = new FactoryDefault();

    $di->set('router',function() {
        require '../app/config/routes.php';
        return $router;
    });

    // Setup the view component
    $di->set('view', function(){
        $view = new View();
        $view->setViewsDir('../app/views/');
        return $view;
    });

    // Setup a base URI so that all generated URIs include the "tutorial" folder
    $di->set('url', function(){
        $url = new UrlProvider();
        $url->setBaseUri('/');
        return $url;
    });

    // Handle the request
    $application = new Application($di);

    echo $application->handle()->getContent();

} catch(\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
}


32.7k

It looks ok. Can you paste your Apache/Nginx configuration for this host ? Also, You might want to use groups:

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

$noauth = new \Phalcon\Mvc\Router\Group([
    'controller' => 'noauth'
]);

$noauth->setPrefix('/');

$noauth->addGet('', ['action' => 'home']);
$noauth->addGet('masses', ['action' => 'masses']);

$router->mount($noauth);

return $router;


9.8k
Accepted
answer
edited May '15

I AM DUMB....

rewrite was not enabled. Thank you for your time and sorry for being dumb.

If anyone else has this problem, run:

sudo a2enmod rewrite

sudo service apache2 restart

...assuming you're on ubuntu/apache