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

Problems upgrading from Phalcon 2 -> 3

What I'm doing is basically ripping off the Vokuro code and changing a couple things, but it's not working for me. The "default" route is working, but the /login and /pricing routes return a 404. mod_rewrite is enabled.

router.php:

    <?php
    /*
     * Define custom routes. File gets included in the router service definition.
     */
    $router = new Phalcon\Mvc\Router();

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

    $router->addGet('/pricing',[
    'controller' => 'noauthpages',
        'action' => 'pricing'
    ]);

    $router->setDefaults([
        'controller' => 'noauthpages',
        'action'     => 'index'
    ]);

    return $router;

services.php:

    <?php
    use Phalcon\Mvc\View;
    use Phalcon\Crypt;
    use Phalcon\Mvc\Dispatcher;
    use Phalcon\Mvc\Url as UrlResolver;
    use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
    //use Phalcon\Mvc\Model\Metadata\Files as MetaDataAdapter;
    use Phalcon\Session\Adapter\Files as SessionAdapter;
    use Phalcon\Logger\Adapter\File as FileLogger;
    use Phalcon\Logger\Formatter\Line as FormatterLine;
    /**
     * Register the global configuration as config
     */
    $di->setShared('config', function () {
        return include APP_PATH . "/config/config.php";
    });
    /**
     * The URL component is used to generate all kind of urls in the application
     */
    $di->setShared('url', function () {
        $config = $this->getConfig();
        $url = new UrlResolver();
        $url->setBaseUri($config->application->baseUri);
        return $url;
    });
    /**
     * Setting up the view component
     */
    $di->set('view', function () {
        $config = $this->getConfig();
        $view = new View();
        $view->setViewsDir($config->application->viewsDir);
        return $view;
    }, true);
    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $di->set('db', function () {
        $config = $this->getConfig();
        return new DbAdapter([
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname
        ]);
    });
    /**
     * If the configuration specify the use of metadata adapter use it or use memory otherwise
     */
    /*
    $di->set('modelsMetadata', function () {
        $config = $this->getConfig();
        return new MetaDataAdapter([
            'metaDataDir' => $config->application->cacheDir . 'metaData/'
        ]);
    });
    */
    /**
     * Start the session the first time some component request the session service
     */
    $di->set('session', function () {
        $session = new SessionAdapter();
        $session->start();
        return $session;
    });
    /**
     * Crypt service
     */
    $di->set('crypt', function () {
        $config = $this->getConfig();
        $crypt = new Crypt();
        $crypt->setKey($config->application->cryptSalt);
        return $crypt;
    });
    /**
     * Dispatcher use a default namespace
     */
    $di->set('dispatcher', function () {
        $dispatcher = new Dispatcher();
        $dispatcher->setDefaultNamespace('Zipline\Controllers');
        return $dispatcher;
    });
    /**
     * Loading routes from the routes.php file
     */
    $di->set('router', function () {
        return require APP_PATH . '/config/routes.php';
    });
    /**
     * Logger service
     */
    $di->set('logger', function ($filename = null, $format = null) {
        $config = $this->getConfig();
        $format   = $format ?: $config->get('logger')->format;
        $filename = trim($filename ?: $config->get('logger')->filename, '\\/');
        $path     = rtrim($config->get('logger')->path, '\\/') . DIRECTORY_SEPARATOR;
        $formatter = new FormatterLine($format, $config->get('logger')->date);
        $logger    = new FileLogger($path . $filename);
        $logger->setFormatter($formatter);
        $logger->setLogLevel($config->get('logger')->logLevel);
        return $logger;
    });

loader.php:

    <?php

    use Phalcon\Loader;

    $loader = new Loader();

    /**
     * We're a registering a set of directories taken from the configuration file
     */
    $loader->registerNamespaces([
        'Zipline\Models' => $config->application->modelsDir,
        'Zipline\Controllers' => $config->application->controllersDir,
        //'Zipline\Forms' => $config->application->formsDir,
        'Zipline' => $config->application->libraryDir
    ]);

    $loader->register();

    // Use composer autoloader to load vendor classes
    require_once BASE_PATH . '/vendor/autoload.php';

I didn't change anything on the index.php page.

Anyway, not sure why the default route works, but the others don't.



17.5k
edited Aug '16

Also, the .htaccess files in the root and in /public are exactly what are shown in the Vokuro github:

Options +FollowSymLinks -MultiViews -Indexes

DirectoryIndex index.html index.php

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>

and

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>


17.5k

My controller looks like this... Pretty simple:

    <?php namespace Zipline\Controllers;

    use Phalcon\Http\Response;
    use Phalcon\Http\Request;

    class NoauthpagesController extends ControllerBase {

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

        public function indexAction() {
            $user = $this->session->get('auth_user');
            if (!empty($user)) {
                $this->view->pick("auth/zipline");
            } else {
                $this->view->pick("no_auth/login");
            }
        }

        public function loginAction() {
            $this->view->pick("no_auth/login");
        }

        public function pricingAction() {
            $this->view->pick("no_auth/pricing");
        }

    }

ControllerBase.php is empty, but there for future use.



17.5k
Accepted
answer

Fixed it. Had to change the the Apache virtual host:

<VirtualHost *:80>
    DocumentRoot /var/www/html/zipline/public
        <Directory /var/www/html/zipline/public>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order allow,deny
                Allow from all
        </Directory>
    ServerName localhost:3000
    ServerAlias localhost
    ErrorLog /var/www/log/zipline_error.log
    CustomLog /var/www/log/zipline_access.log combined
</VirtualHost>