0
votes

I'm new on Zend Framework 2 coding, and I've a problem with my layouts and controllers :

When I browse vhost/, it renders my layout and the index.phtml view; so everything is OK !

But, when I browse vhost/cv, it only renders the cv.phtml without my layout...

Here is my code

module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Portfolio\Controller\Home' => 'Portfolio\Controller\HomeController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'home' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller'    => 'Portfolio\Controller\Home',
                        'action'        => 'index',
                    ),
                ),
            ),
            'cv' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/cv',
                    'defaults' => array(
                        'controller'    => 'Portfolio\Controller\Home',
                        'action'        => 'cv',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

HomeController.php

class HomeController extends AbstractActionController
{

    public function indexAction()
    {
        return new ViewModel();
    }

    public function cvAction()
    {
        return new ViewModel();
    }

}

I tried using 2 controllers, using child routes, and none of these worked...

Can someone tell me what is wrong with my code ?

Thanks, McSIME

EDIT :

layout.phtml

<?php echo $this->doctype(); ?>

<html lang='fr'>
    <head>
        <meta charset='utf-8' />

        <link rel='stylesheet' type='text/css' href='<?php echo $this->basePath() . '/css/bootstrap.min.css' ?>' />
        <link rel='stylesheet/less' type='text/css' href='<?php echo $this->basePath() . '/css/base.less'; ?>' />
        <link rel='shortcut icon' href='<?php echo $this->basePath() . '/img/favicon.ico'; ?>' />

         <?php echo $this->headScript()
            ->prependFile($this->basePath() . '/js/ga.js')
            ->prependFile($this->basePath() . '/js/global.js')
            ->prependFile($this->basePath() . '/js/less-1.4.1.min.js')
            ->prependFile($this->basePath() . '/js/bootstrap.min.js')
            ->prependFile($this->basePath() . '/js/jquery-1.10.2.min.js')
        ; ?>

        <title>Test</title>
    </head>
    <body data-spy='scroll' data-target='#header' data-offset='200'>


        <?php require($this->basePath() . 'temp-old/views/shared/header.php') ?>

        <div id='content'>
            <?php echo $this->content; ?>

            <?php require($this->basePath() . 'temp-old/views/shared/footer.php') ?>
        </div>
    </body>
</html>

cv.phtml

<h1 class='heading-1 small-separator'>
    // My heading
</h1>
<section>
    // All my CV
</section>

index.phtml

<!-- Presentation -->
<?php require('_presentation.php'); ?>
<!-- End Presentation -->

<!-- Prestations -->
<?php require('_prestations.php'); ?>
<!-- End Prestations -->

<!-- Contact -->
<?php require('_contact.php'); ?>
<!-- End Contact -->
1
Sounds kinda weird, I'd argue to just re-install / update the ZendSkeletton and then go from there. Never had such a thing happening to me.Sam
If you're using ZendFramework 2 Skeleton Application, There's a layout but still empty. What you see in (vhost/) is a view script from index.phtml.Philip F
Looks like an error in the view. Make sure that errors are being displayed.akond
Please post both view scripts so we can find out where the error is. Probably in cv.phtml you have an error.Jurian Sluiman
So I've added view scripts : layout.phtml, cv.phtml and index.phtml My application is a full copy paste from ZF2 Skeleton Application, except config, controllers and views.McSIME

1 Answers

0
votes

Ok I resolved my problem, so it was really dummy (yes I am) error:

I've put a link in to cv.phtml:

<a href='$this.basePath()'>my link</a>

So each time I tried to render both layout + view, the view script thrown an exception : "$this.basePath() is an unknown function"

Me yesterday : "Whaaaaat ?! And so why ? I used it into index.phtml !"

Me today : "What a mess ! PHP calls object methods using a ->, not a '.'.

So the view script cv.phtml doesn't have any error left and it works as it should with my layout.

Thanks to all for your replies, and to Jurian Sluiman that confirmed me that there was probably an error into my cv.phtml.