How to add Custom route into your zend framework skeleton application

Jun 6, 2013   //   by kdecom   //   zend framewok 2, ZEND FRAMEWORK, zend framework tutorial, zf2  //  No Comments

How to add Custom route into your zend framework skeleton application

 

“FIRST I WOULD LIKE TO SAY IS IF YOU WANT TO KNOW ONLY ABOUT CUSTOM ROUTE THEN SKIP THE FIRST PART AND LOOK BOTOOM”

If you like a Zend Framework. Then surely you will know it they released a latest ZF2 version. It has a lot of big changes regarding developer point of view.

I just download the Zend Skeleteton application and did a test. first problem that i was thinking is.

Is there a way for me to move the index.php and .htaccess into the root folder so i can access the website with http://www.localhost.com/zf

It was a very simple fix for me. I did moved both file into the root folder. I get an error saying

Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.' in ****///***init_autoloader.php on line 48

It was a simple fix/solution for this bug.

In index.php file.

replace the code like this.
// NOTICE BELOW I DID REMOVED THE DIRNAME 
chdir(__DIR__);

require 'init_autoloader.php';

Zend\Mvc\Application::init(require 'config/application.config.php')->run();

After that i did try again and guess it works but css is not able to load it.

there is two thing that you can do to fix that.

  1. Move all folders and file from public to roo. (PERSONALLY I DON’T PREFER).
  2. I did changed the CSS path into the layout files.

Simple i open a file /module/Application/view/layout/layout.phtml

Replace the code like this.

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

<html lang="en">
    <head>
        <meta charset="utf-8">
        <?php echo $this->headTitle('ZF2 '. $this->translate('Skeleton Application'))->setSeparator(' - ')->setAutoEscape(false) ?>

        <?php echo $this->headMeta()->appendName('viewport', 'width=device-width, initial-scale=1.0') ?>

        <!-- Le styles -->
        <?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/public/img/favicon.ico'))
                        ->prependStylesheet($this->basePath() . '/public/css/bootstrap-responsive.min.css')
                        ->prependStylesheet($this->basePath() . '/public/css/style.css')
                        ->prependStylesheet($this->basePath() . '/public/css/bootstrap.min.css') ?>

        <!-- Scripts -->
        <?php echo $this->headScript()->prependFile($this->basePath() . '/public/js/html5.js', 'text/javascript', array('conditional' => 'lt IE 9',))
                                      ->prependFile($this->basePath() . '/public/js/bootstrap.min.js')
                                      ->prependFile($this->basePath() . '/public/js/jquery.min.js') ?>

    </head>
    <body>
        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Skeleton Application') ?></a>
                    <div class="nav-collapse collapse">
                        <ul class="nav">
                            <li class="active"><a href="<?php echo $this->url('home') ?>"><?php echo $this->translate('Home') ?></a></li>
                        </ul>
                    </div><!--/.nav-collapse -->
                </div>
            </div>
        </div>
        <div class="container">
            <?php echo $this->content; ?>
            <hr>
            <footer>
                <p>© 2005 - <?php echo date('Y') ?> by Zend Technologies Ltd. <?php echo $this->translate('All rights reserved.') ?></p>
            </footer>
        </div> <!-- /container -->
        <?php echo $this->inlineScript() ?>
    </body>
</html>

If you notice carefully i just added a “/public” in front of all css and js files.

 

I want to add a testAction into my controller and i was willing to run it. I did added an empty testAction like this..

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

Then i try to execute http://www.localhost.com/zf/application/index/test

It works perfectly file for me. (NOTE here Make sure you add a file test.phtml file into your view folder)

Just for my concern i want to to execute http://www.localhost.com/zf/test and i want it to execute my testAction.

that’s where i get tipped off. Here is my solution.

LOOK HERE FOR THE CUSTOM ROUTE

I did open a config file for the Application module.

added a new route after the default root like this ..

 'route'    => '/test',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'test',
                    ),

Just want to say is If you are trying to do same then make sure you add at right point. In my case it was around line number 13.

I hope you get the idea of how to added a custom route into your Zf2 based application.

If anyone got any problem please feel free to leave a comment here.

Leave a comment

css.php