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

Issue with loading a partial view

Hi guys,

I have an issue when loading a partial template. My styling and JavaScript files all give a 404 not found error.

My css files all live in the inc/styles.phtml file.

This only happens on a specific controller called "Section". When I access my /section/a page there is no styling. The section controller source :

    <?php
    namespace My\Controllers;

    use \Phalcon\Mvc\Controller;

    class SectionController extends \Phalcon\Mvc\Controller {

    public function indexAction()
    {     
       // redirect any index responses back to 404 controller
    }        

    public function showAction()
    {        
        $section = $this->dispatcher->getParam("section");                
        $this->view->setVar("section", $section);        
        //Load the correct view
        $this->view->pick('section/' . $section);                
     }

The inc/styles.phtml file :

    <!-- General Style Sheets -->
    <link rel="stylesheet" type="text/css" href="assets/css/font-awesome.min.css">

I have notice the path to the resources is completely wrong. The /section name is added in the url.

Here is the views/section/a.phtml file source :

    <!doctype html>
    <html
    <head>
        <title>Title</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- could this be the problem?? -->
        <?php         
        $this->partial("inc/styles") 
        ?>
    </head>    
    <body>
        <h1> Hello Section :  <?php echo $section; ?> </h1>
    </body>
    </html>


43.9k
edited Sep '14

Hi,

instead of calling your assets via a partial, a better approach is to use phalcon's assets management::


public function showAction()
{        
    $section = $this->dispatcher->getParam("section");                
    $this->view->setVar("section", $section);        
    //Load the correct view
    $this->view->pick('section/' . $section); 
        //Add some local CSS resources
        $this->assets
            ->addCss('assets/css/font-awesome.min.css');
 }
  //that code in your head section:

  <?php $this->assets->outputCss() ?>


22.8k
edited Sep '14

Hi, my problem is not specifc to sytles sheets and JavScript files. I have a view with 2 other partials inside eg.

$this->view->partial('section/header');
$this->view->partial('section/footer');

And stylesheets inside the header.phtml will be referenced by the wrong URL, the urls are prefixed with the controller name for some reason so routes like this wont work :

/section/a

but a route like this will work

/section


43.9k
Accepted
answer

And stylesheets inside the header.phtml will be referenced by the wrong URL, the urls are prefixed with the controller name for some reason

that's why using phalcon's asset manager will solve these troubles for you ;-) Just try it



22.8k

Thanks man, this worked for me. I created a BaseController with all the assets inside then extended all my child controllers from basecontroller.



43.9k

good ! Assets management by phalcon offers you a lot of nice features: collections, minification and so on ...