Newbie trying to set a layout

So im trying to set a layout for my project but when i try to use $this->view->setTemplateAfter('header'); it just throws the following error: PhalconException: View '/home/user/www/project/public/layouts/header' was not found in the views directory

heres a few files:

/////home/user/www/project/public/index.php

error_reporting(E_ALL);
try {
    //Read the configuration
    $config = new Phalcon\Config\Adapter\Ini(__DIR__ .'/../app/config/config.ini');
    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(
        array(
            __DIR__ .$config->application->controllersDir,
            __DIR__ .$config->application->pluginsDir,
            __DIR__ .$config->application->libraryDir,
            __DIR__ .$config->application->modelsDir,
        )
    )->register();

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

    //Setup the database service
    $di->set('db', function(){
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->name
       ));
    });

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir( __DIR__ .$config->application->viewsDir);
        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($config->application->baseUri);
        return $url;
    });

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

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

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

/////home/user/www/project/app/config.ini

[database]
host     = localhost
username = root
password = secret
name     = project

[application]
controllersDir = /../app/controllers/
modelsDir      = /../app/models/
viewsDir       = /../app/views/
pluginsDir     = /../app/plugins/
libraryDir     = /../app/library/
baseUri        = /project/

/////home/user/www/project/app/index.phtml

<?php use Phalcon\Tag; ?>
<html>
<head>

</head>
<body>
 <?php echo $this->getContent() ?>

<div id="content-container" class="container-fluid"></div>
</body>


</html>

/////home/user/www/project/app/controllers/IndexController.php

class IndexController extends \Phalcon\Mvc\Controller
{
    protected function initialize()
    {
     $this->view->setTemplateAfter('header'); 
    }

    public function indexAction()
    {


    }

}

/////home/user/www/project/app/views/layouts/header.phtml

<?php use Phalcon\Tag; ?>
<nav class="navbar navbar-inverse" role="navigation">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">project</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li><a id="admin-link" >Admin</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

after some testing i made it work by changing $view->setViewsDir("../app/views"); and $url->setBaseUri("/project/"); it seems im doing something wrong with the .ini file but i cant seem to get whats wrong

<?php
error_reporting(E_ALL);
try {
    //Read the configuration
    $config = new Phalcon\Config\Adapter\Ini(__DIR__ .'/../app/config/config.ini');
    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(
        array(
            $config->application->controllersDir,
            $config->application->pluginsDir,
            $config->application->libraryDir,
            $config->application->modelsDir,
        )
    )->register();

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

    //Setup the database service
    $di->set('db', function(){
        return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->name
       ));
    });

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
      //  $view->setViewsDir($config->application->viewsDir);
        $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($config->application->baseUri);
        $url->setBaseUri("/neutronPc/");
        return $url;
    });

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

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

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


1.4k
edited Oct '14

What i think you are doing wrong is the following when attaching the services:

Original Code

    //Setup the view component
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
      //  $view->setViewsDir($config->application->viewsDir);
        $view->setViewsDir("../app/views");
        return $view;
    });

Updated Code

    //Setup the view component
    $di->set('view', function() use ($config) {
        $view = new \Phalcon\Mvc\View();
      //  $view->setViewsDir($config->application->viewsDir);
        $view->setViewsDir("../app/views");
        return $view;
    });

You have to pass the config variable into the function. I hope that resolves your issue.