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

volt basics - how to display static pages under a folder level

Hi Guys, I am creating a basic POST app using phalcon and would appreciate a little help with dispalying static pages. I initially started the project using phalcon devtools so it appears to have enabled vold by default although I cant find where it is enables so can you point me to where it is enabled. I then created a routes.php file under the config folder to point to a PagesController.php under the controllers folder. The PagesController.php file points to an "about page" and a "contact page" very simple. Here is the code:

<?php use Phalcon\Mvc\Controller; class PagesController extends Controller { public function aboutAction() { } public function contactAction() { } }

I have the about.volt file and the contact.volt file within my views folder under a pages folder which the controller loads depending on the url. I would like to organise all my static pages under a folder called pages because i have a lot of them but doing it this way means the URL will be news/pages/about or news/pages/contact. Is there a way to prevent displaying pages in the url, or is there a better way to do this. Thanx Also can you just load a view from the routes.php file for a static page without loading a controller or is this not a good practice. Can someone please tell me what the phtml files for volt are and where and when to use them or is it not really used in phalcon version 3. Any examples would be appreciated. Many Thanks.

edited Nov '16

in your action:

use Phalcon\Mvc\Controller;
class PagesController extends Controller {
    public function aboutAction() {
        $this->view->pick('your/view/file');
    }
    public function contactAction() {
        $this->view->pick('your/view/file');
    }
}

Phalcon can handle many render engines simultaneously;

.volt files are rendered by the volt engine

.phtml files are treated as regular PHP files

You could create your own engine that simply takes a regular .html file and displays it as-is, without parameter substitution. But performance-wise there is minimal difference between either method. You could just put your static html content in a .volt file, the response time will grow by like 5-10ms at most.



43.9k
edited Nov '16

Hi,

if you do not want to create a specific action for every page you have to display, you can do something like that:


// PagesController.php

public function displayAction($pageName)
{
    $this->view->pick('path/to/your pages/folder/'.$pageName);
}

// maybe define a specific route if you do not want to use mywebsite.com/pages/display/pageName:

$router->add(
    "page/:pageName",
    [
        "controller" => "pages",
        "action" => "display",
        "params => 1
    ],
);

// you can now acces to your static pages with mywebsite.com/page/pageName


3.2k

Thanks for the replies its great to get some help. I still notice that when creating a controller or pages controller to display pages it will always diaplay the URL with mywebsite.com/page/pageName or mywebsite.com/page/about. Is there any way to have it display mywebsite.com/about. Im thinking if I can just have a view loaded from the routes page I will get what I want. Does anyone know how to do this. Yhanx



43.9k
edited Nov '16

Hi,

Is there any way to have it display mywebsite.com/about

yes of course, just remove the "page" in the router->add(...) definition. But be carefull because if you've got others controllers (saying TestController.php) in your application you may experience "error 404, not found" when trying to access mywebsite.com/test.

More about routing here: https://docs.phalcon.io/en/latest/reference/routing.html

Since you can add many routes as you need using the add() method, the order in which routes are added indicate their relevance, latest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.

and : https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior