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

Turorial 1 -> Not being served signup/index.phtml

Hi,

Noob here.

I have followed tutorial 1 and have got stuck after setting the SignupController.php and view/signup/index.phtml

I have found that when clicking on the sign up link it requests "GET signup" but serves the root index.phtml not the one in /tutorial/app/views/signup/index.phtml. In bootstrap I set $url->setBaseUri('/'); as when it was tutorial I got "PhalconException: TutorialController handler class cannot be loaded"

I have found that if the controller SignupController.php isn't present then I get: PhalconException: SignupController handler class cannot be loaded. So I know that its not a problem with the controller not being seen.

What is happening is that when I click on the signup link I am getting served the route index.phtml

Please can anyone help because as far as I can see there is something wrong in the default routing performed by Phalcon.

Here is my setup: cat /etc/apache2/sites-available/000-default.conf


<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin [email protected]
    DocumentRoot /var/www

    <Directory /var/www/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>    

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

cat /etc/apache2/sites-available/tutorial.conf

<VirtualHost *:80>

    ServerAdmin [email protected]
    DocumentRoot "/var/www/nbprojects/tutorial/public"
    DirectoryIndex index.php
    ServerName tutorial.host
    ServerAlias www.tutorial.host

    <Directory "/var/www/nbprojects/tutorial/public">
        Options All
        AllowOverride All
        Allow from all
    </Directory>

<IfModule mod_rewrite.c>

    <Directory "/var/www/nbprojects/tutorial">
        RewriteEngine on
        RewriteRule  ^$ public/    [L]
        RewriteRule  (.*) public/$1 [L]
    </Directory>

    <Directory "/var/www/nbprojects/tutorial/public">
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
    </Directory>

</IfModule>

</VirtualHost>
<?php

try {

    $debug = new \Phalcon\Debug();
    $debug->listen();

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

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

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\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 \Phalcon\Mvc\Url();
        $url->setBaseUri('/');
        return $url;
    });

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

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

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

everything looks good.. what is your code in IndexController.php and SignupController.php?

Pleas use code style like described here: https://forum.phalcon.io/discussion/1256/how-to-display-code-styles-in-forum for your code ;)



47.7k
edited Apr '14

IndexController.php

    <?php

    class IndexController extends \Phalcon\Mvc\Controller
    {

            public function indexAction()
            {    
            }

    }

SignupController.php

    <?php

    class SignupController extends \Phalcon\Mvc\Controller
    {

        public function indexAction()
        {
        }

    }

ls -R tutorial

    tutorial/:
    app  public

    tutorial/app:
    controllers  models  views

    tutorial/app/controllers:
    IndexController.php  SignupController.php

    tutorial/app/models:

    tutorial/app/views:
    index.phtml  signup

    tutorial/app/views/signup:
    index.phtml

    tutorial/public:
    css  img  index.php  js

    tutorial/public/css:

    tutorial/public/img:

    tutorial/public/js:

Sorry I misunderstood your problem. Adding this line to view/index.phtml should serve the problem:

<?php echo $this->getContent() ?>


47.7k

Yes it does. So now I see that it serves in every call where there is a corresponding file with correctly named controller class:

app/views/index.phtml

When calling 'GET signup' it doesn't get the content of the corresponding view in app/views/signup/index.phtml.

It does display the app/views/index.phtml and where:

<?php echo $this->getContent() ?>

It shows the controller class is loaded and but only one page is getting served.

Is there something wrong with the rewrite rule in the apache config or is there a problem with getContent in bootstrap?

I'm a bit stumped here.

Everything works as it should. This is the behavior of Phalcons template engine. If app/views/index.phtml exists, Phalcon automatically uses it as your Main layout by default and getContent() injects the content (in your case: app/views/signup/index.phtml). If you delete the app/views/index.phtml, everything will still work (you can try it). I hope i could help.

read more about views: https://docs.phalcon.io/en/latest/reference/views.html#hierarchical-rendering



47.7k

This is good to know and it makes sense. (I tried it) Thank you for your help.

However I am left a little unsure as I have been following this: https://docs.phalcon.io/en/latest/reference/tutorial.html



47.7k

Additionally I should have mentioned that the posted behaviour was found in osx Mavericks with Apache/2.2.24 (Unix) .

I have since installed into Ubuntu 12.04.3 LTS with Apache/2.2.22 (Ubuntu) and found that in this environment it behaves as predicted in: https://docs.phalcon.io/en/latest/reference/tutorial.html



47.7k
Accepted
answer
edited Apr '14

In osx I succesfully setup my php and apache environment (so not as default in osx) which also allows me to compile the correct phalcon.so for my running php environment.

I used Patrick Bougies instructions to the letter to aid setup of this environment to feel a little more in control of my dev environment.

https://mac-dev-env.patrickbougie.com

I had the same error mentioned above. The signup view didn't load. I realized that I have created the first view in "/views" directory initially, but in tutorial says "/views/index/" directory instead. I fixed this mistake and could load the signup page without problem!