티스토리 뷰
1. Request Routing
$page_name = Request::instance()->param('page');
$this->request->response = View::factory('page/'.$page_name)
->bind('menu', $menu);
$menu = Request::factory('static/menu')->execute()->response;
view 안에서의 사용
execute()->response ?>
2. Route 의 흐름을 무시하도록 라우팅 <br />
기본
controller/action/id <br />
이라면
controller/action/id/and/anything/else/you/want/to/add/at/random
요렇게 하려면
Route::set('default', '(<controller>(/<action>(/<id>(/<stuff>))))', array('stuff' => '.*'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
요런식으로 라우팅 Set 해주면 됀다.
3. static 페이지 Routing
Route::set('static', '<page>', array('page' => 'about|faq|locations'))
->defaults(array(
'controller' => 'page',
'action' => 'static',
));
* This route supports any number of pages.
* Each page accessible the following URL where page_name is the
* name of the page you want to load.
* http://example.com/page/page_name
*/
Route::set('static2', 'page/<page>', array('page' => '.*'))
->defaults(array(
'controller' => 'page',
'action' => 'static',
));
4. 다국어 Routing 설정
http://example.com/en-us/products // For English
http://example.com/fr-fr/products // For French
Route::set('default', '(<lang>/)(<controller>)(/<action>(/<id>))', array('lang' => '(en-us|fr-fr|nl-nl)', 'id'=>'.+'))
->defaults(array(
'lang' => 'en-us',
'controller' => 'welcome',
'action' => 'index',
));
$this->template->title = __('Hello World');
// Load a language specific view
$this->template->content = View::factory('page/'.I18n::$lang.'/index');
//OR use the same template for all languages
//$this->template->content = View::factory('page/index');
5. sub-directiones 설정
/home/kerkness/application/classes/controller/foo/bar.php
url 호출
http://example.com/foo/bar/action/id
라우팅
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))')
->defaults(array(
'directory' => 'foo',
'controller' => 'bar',
'action' => 'index',
));
멀티 sub-direcories 설정
http://example.com/my/sub/directory/controller/action/id
Route::set('default', '(<directory>(/<controller>(/<action>(/<id>))))', array('directory' => '.+?'))
->defaults(array(
'directory' => 'foo',
'controller' => 'bar',
'action' => 'index',
));
6. redirect 의 사용
Request::instance()->redirect('http://kohanaphp.com');
Request::instance()->redirect('controller/action');
Request::instance()->redirect( Route::get('demo-sample')->uri(array('category'=>'books', 'id'=>5)) );
7. 테스트 route
// Here you define the routes
// ...
Route::set('post', 'post/<id>', array('id' => '[\d]+'))
->defaults(array(
'controller' => 'post',
'action' => 'index',
));
// ...
// The URI to test
$uri = 'post/1';
// This will loop trough all the defined routes and
// tries to match them with the URI defined above
foreach (Route::all() as $r)
{
echo Kohana::debug($r->matches($uri));
}
array(3) (
"id" => string(1) "1"
"controller" => string(4) "post"
"action" => string(5) "index"
)
array(2) (
"controller" => string(4) "post"
"action" => string(1) "1"
)
'웹개발 > Php' 카테고리의 다른 글
| Kohana Validation 체크 (0) | 2011.08.26 |
|---|---|
| Kohana Query Builder (0) | 2011.08.26 |
| Kohana 3.2 Config 얻기.. (0) | 2011.08.24 |
| Kohana 3.2 Custom Error Page 만들기. (1) | 2011.08.24 |
| PHP 비트 연산자 사용. (0) | 2011.07.17 |
댓글