2012-10-22 20 views
1

我有ZF 1,在那裏我工作Bootstrap.php與大量的路由和其他preDispatch的東西。ZendFramework 2 - 我如何做類似ZF1的Bootstrap.php?

但是在ZF2中沒有Bootstrap.php概念了?或者我的意思是我如何在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); 
    } 
+0

Afaik他們強烈推薦**不**使用動態路由了。 – pebbo

回答

1

一個ZF2最好的功能是什麼,其實我討厭首先,這是路線。這是很好的討厭,因爲現在你需要設置所有模塊的路由。

部分理解ZF2(更快)是理解模塊。如果你能克服這一點,你將開始更快地適應。 (至少這對我來說是如此)。那麼,ZF2是一個模塊?什麼!

無論如何,每個模塊和應用程序的所有配置文件最終都會在Zend Framework中合併,這意味着您可以在任何地方真正定義路由。

也就是說,您不需要「引導」您的路線了,因爲這是您的ModuleName/config/module.config.php文件的一部分。現在。

現在,我不是內ZF2的regex路線的專家,但它會是這樣的:

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

某處在那裏您定義的正則表達式。另外,我在他們的文檔看到,你也可以手動定義一個正則表達式的路由:如果您使用的骨架

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); 

你應該能夠在onBootstrap()應用模塊中添加此作爲一項服務或把它應用。請記住,這是他們的的例子,再次,我不是這方面的專家。 Here is some more information.

希望這會有所幫助!

+1

謝謝。實際上ZF2現在就像CakePHP,在蛋糕中它曾經是:'Router :: connect('/ terms-condition',array('controller'=>'cms_pages','action'=>'index',2) );'(仍然我喜歡ZF1的老方法) – YumYumYum

+1

噢,不錯,我不知道你可以在Cake中做到這一點。順便說一句,你很受歡迎!有些關於ZF1的東西我也更喜歡,但是我越適應ZF2,我越喜歡它。 –

相關問題