ZendFramework 2 - how can i do the similar Bootstrap.php like in ZF1?

932 views Asked by At

I have ZF 1 where i got working Bootstrap.php with lots of routing and other preDispatch stuff.

But in ZF2 there is no Bootstrap.php concept anymore? Or i mean how can i do this same in Zf2?

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

  protected function _initPdispatch() {
    $this->bootstrap('frontController');
    require_once APPLICATION_PATH . '/controllers/plugin/LanguageSelector.php';
    $plugin = new LanguageSelector();
    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin($plugin);
    return $plugin;
  }

  protected function _initRoutes() {
    $front = Zend_Controller_Front::getInstance();
    $router = $front->getRouter();
    $dynamic1 = new Zend_Controller_Router_Route(
                    '/:variable1',
                    array(
                        'controller' => 'router',
                    ),
//              array('variable1' => '^[a-zA-Z0-9_-]*$')
                    array('variable1' => '^[\w.-]*$')
    );
    $router->addRoute('dynamic1', $dynamic1);
  }
1

There are 1 answers

2
Yes Barry On BEST ANSWER

One of the best features of ZF2 is something that I actually hated at first, which are the routes. It's both great and annoying because now you're required to set the routes for all of your modules.

Part of understanding ZF2 (more quickly) is understanding modules. If you can get past this, you will begin to adapt much more quickly. (At least that's how it was for me). So, what in ZF2 is a module? Anything!

Anyway, all of the config files for every module and for the application eventually get merged within the Zend Framework, so that means you can define routes anywhere really.

That said, you don't need to "bootstrap" your routes anymore as that is part of your ModuleName/config/module.config.php file. now.

Now, I'm not an expert on regex routes within ZF2, but it would be something like:

// MyModule/config/module.config.php
return array(
    'router' => array(
        'routes' => array(
            'dynamic1' => array(
                'type' => 'regex',
                'options' => array(
                    'route' => '/[:variable1]'
                )
            )
        )
    )
);

Somewhere in there you define the regex. Additionally, I saw in their docs that you can also define a regex route manually:

use Zend\Mvc\Router\Http\Regex;

// ...

$route = Regex::factory(array(
    'regex' => '/blog/(?<id>[a-zA-Z0-9_-]+)(\.(?<format>(json|html|xml|rss)))?',
    'defaults' => array(
        'controller' => 'Application\Controller\BlogController',
        'action'     => 'view',
        'format'     => 'html',
    ),
    'spec' => '/blog/%id%.%format%',
));

$router->addRoute($route);

You should be able to add this as a service or put it in onBootstrap() within the Application module if you're using the Skeleton Application.

Keep in mind, that was their example and again, I'm not an expert on this. Here is some more information.

Hope this helps!