facebook twitter hatena line email

Php/zend framework/zend router

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ルータ

~/test/3にアクセスした場合、controller => index, action => index, page => 3にアクセスされるスクリプト

index.php(フロントコントローラー)

require_once 'Zend/Controller/Front.php';
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute('router1',
   new Zend_Controller_Router_Route(
   'test/:id',
   array(
       'controller' => 'index',
       'action' => 'index'
   ),
   array('id' => '\d+')
));
$front->setControllerDirectory('../application/controllers');
$front->dispatch();

ルータ(ini編

index.php(フロントコントローラー)

define('APPLICATION_ENV', 'production');
require_once 'Zend/Config/Ini.php'
$ini = new Zend_Config_Ini('../configs/route.ini',
                             APPLICATION_ENV);
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($ini, 'routes');

route.ini

[production]
routes.router1.route = "test/:id"
routes.router1.defaults.controller = "index"
routes.router1.defaults.action = "index"
routes.router1.reqs.id = "\d+"

zend_applicationでrouterを使う

application.iniに以下を設定

resources.router.routes.route_id.route = "/login"
resources.router.routes.route_id.defaults.module = "user"
resources.router.routes.route_id.defaults.controller = "login"
resources.router.routes.route_id.defaults.action = "index"

日本語をrouterする場合

resources.router.routes.rss_new.route = "list/:word"
resources.router.routes.rss_new.defaults.module = "default"
resources.router.routes.rss_new.defaults.controller = "list"
resources.router.routes.rss_new.defaults.action = "index"
var_dump($this->_request->word);

ドット混じりの英語、英語のみ、数字のみをrouteする場合

resources.router.routes.route_id.route = "/bbs/:domain/test/read.cgi/:ita_id/:sure_id"
resources.router.routes.route_id.defaults.module = "bbs"
resources.router.routes.route_id.defaults.controller = "index"
resources.router.routes.route_id.defaults.action = "index"
resources.router.routes.route_id.reqs.domain = "[\w\.]+"
resources.router.routes.route_id.reqs.ita_id = "[\w]+"
resources.router.routes.route_id.reqs.sure_id = "[\d]+"

ドメインでmoduleを切り替えるroute設定方法

mobile.example.jpとmobile-test.localhostをmobileモジュールへ

resources.router.routes.route_mobile.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.route_mobile.route = ":subdomain.:domain";
resources.router.routes.route_mobile.reqs.domain = "(example.jp|localhost)"
resources.router.routes.route_mobile.reqs.subdomain = "(mobile|mobile-test)"
resources.router.routes.route_mobile.defaults.module = "mobile"
resources.router.routes.route_mobile.defaults.controller = "index"
resources.router.routes.route_mobile.defaults.action = "index"
resources.router.routes.route_mobile.chains.user.type = "Zend_Controller_Router_Route"
resources.router.routes.route_mobile.chains.user.route = "/user/:id"
resources.router.routes.route_mobile.chains.user.defaults.controller = "user"
resources.router.routes.route_mobile.chains.user.defaults.action = "index"
resources.router.routes.route_mobile.chains.user.reqs.id = "\d+"