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.

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>



30.7k
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() ?>


13.7k
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


30.7k
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



13.7k

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



30.7k

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