intro
最近知ったZend FrameworkでのURL記述方法についてです。以下の記事ではベースURLがhttp://localhost/mybase/でことを想定します。またデフォルトのルートクラスを使用した場合です。
1.コントローラでのURL記述例
以下のようにコントローラ内でURLを取得できます。
以下のようにリダイレクトを行えます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $options = array( 'action' => 'myaction', 'controller' => 'mycontroller', 'module' => 'mymodule' ); // '/mybase' $this->getRequest()->getBaseUrl(); // '/mycontroller/myaction' $this->_helper->url('myaction', 'mycontroller'); // '/mycontroller/myaction' $this->_helper->url('myaction', 'mycontroller', 'default'); // '/mymodule/mycontroller/myaction' $this->_helper->url('myaction', 'mycontroller', 'mymodule'); // '/mybase/mymodule/mycontroller/myaction' $this->_helper->url->url($options); |
以下のようにリダイレクトを行えます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// redirect to /mybase/mycontroller/myaction $this->_redirect('/mycontroller/myaction'); // redirect to http://somehost/ $this->_redirect('http://somehost/'); // redirect to /mybase/mycontroller/myaction $this->_helper->redirector('myaction', 'mycontroller'); // redirect to /mybase/mycontroller/myaction $this->_helper->redirector->gotoUrl('/mycontroller/myaction'); // redirect to /my.html $this->_helper->redirector ->setPrependBase(false) ->gotoUrl('/my.html'); |
2.ビューでのURL記述例
以下のようにビュー内でURLを取得できます。
1 2 3 4 5 6 7 8 |
<?php $options = array( 'action' => 'myaction', 'controller' => 'mycontroller', 'module' => 'mymodule'); ?> <!-- /mybase/mymodule/mycontroller/myaction --> <?= $this->url($options) ?> |
参考
ビューでの静的なファイルへのURLの記述方法については以下のサイトに関連情報がのせられています。