2013-06-25 1 views
0

연습을 위해 미니 웹샵 시스템을 만들고 있으므로 여러 웹 사이트를 동적으로 만들 수 있습니다. 이 때문에 처리 할 나는 데이터베이스의 URI를 찾아 동적 경로를 가질 수Kohana 3, 동적 URL 감지

Route::set('dynamic_routes', function($uri) 
{ 
    $webshop = DB::select()->from('webshops') 
     ->where('uri', '=', $uri) 
     ->execute() 
     ->get('id', 0); 

    // Check if there is a match 
    if ($webshop > 0) 
    { 
     define('WEBSHOP_ID', $webshop); 

     return array(
      'controller' => 'shop', 
      'action' => 'index', 
      'directory' => 'shop' 
     ); 
    } 
} 
); 

:

나는 현재이 있습니다.

웹샵이 일치하는 경우 웹샵 색인으로 연결됩니다. - 잘 작동합니다.

이제는 "/ myWebshop"과 같이 webshop의 루트 URI에 착륙 할 때만 작동합니다. 나는 두 개의 컨트롤러, 하나의 소위 "가게"등의 소위 "고객"내가 그들을/myWebshop/쇼핑/액션 및/myWebshop/고객이 액세스 할 수 싶습니다/액션이있는 모든 webshops를 들어

여기에 나와있는 문제는 "myWebshop"이 동적이며 "shop"컨트롤러 또는 "customer"컨트롤러에서 동작 함수 방법도 동적이라는 것입니다.

어떻게 동적으로 두 개의 경로를 쓸 수 있습니까?

나는이 후 무엇을 잘 모릅니다
if(strpos($uri, '/')) 
{ 
    // If we have a /, and [1] isn't empty, we know that the user looks for subpage? 
    $expl = explode('/', $uri); 

    if(!empty($uri[1])) 
    { 
     // Set the uri to the first part, in order to find the webshop? 
     $uri = $uri[0]; 
    } 


$webshop = DB::select()->from('webshops') 
    ->where('uri', '=', $uri) 
    ->execute() 
    ->get('id', 0); 

// Check if there is a match 
if ($webshop > 0) 
{ 
    define('WEBSHOP_ID', $webshop); 

    return array(
     'controller' => 'shop', 
     'action' => 'index', 
     'directory' => 'shop' 
    ); 
} 
} 

이 어떻게 동적으로 경로를 생성하고 사용자에게 직접 가리키는 시작 알 수 있습니다 :

이 내가 온 얼마나 걸리나요?

답변

0

당신이 $uri을 위해했던 것과 같은 방식으로 URL에서 컨트롤러와 액션 부분을 보자

return array(
     'controller' => !empty($uri[1]) ? $uri[1] : 'some_default_controller', 
     'action' => !empty($uri[2]) ? $uri[2] : 'some_default_action', 
     'directory' => 'shop' // OR $uri[x] if it's also supposed to be dynamic 
    );