2014-09-09 6 views
0

안녕하세요, 저는 PHP 세계에 처음 왔습니다. 다국어 라우팅을 처리하는 가장 좋은 방법은 무엇입니까? Phalcon php로 웹 사이트를 만들기 시작했습니다. 다음 라우팅 구조가 있습니다. 내가 mywebsite.com/en/ 또는 다른 언어로 같은 디스패처 내 URL을 변경하려는 mywebsite.com/에 갈 때팔콘 php 다국어 라우팅

$router->add('/{language:[a-z]{2}}/:controller/:action/:params', array(
      'controller' => 2, 
      'action' => 3, 
      'params' => 4, 
    )); 

    $router->add('/{language:[a-z]{2}}/:controller/:action', array(
      'controller' => 2, 
      'action' => 3, 
    )); 

    $router->add('/{language:[a-z]{2}}/:controller', array(
      'controller' => 2, 
      'action' => 'index', 
    )); 

    $router->add('/{language:[a-z]{2}}', array(
      'controller' => 'index', 
      'action' => 'index', 
    )); 

내 문제는 예를 들면이다. beforeDispatchLoop에서 처리하는 것이 좋습니다. 나는 최선의 해결책을 찾는다.

/**Triggered before entering in the dispatch loop. 
* At this point the dispatcher don't know if the controller or the actions to be executed exist. 
* The Dispatcher only knows the information passed by the Router. 
* @param Event $event 
* @param Dispatcher $dispatcher 
*/ 
public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher) 
{ 
     //$params = $dispatcher->getParams(); 
     $params = array('language' => 'en'); 
     $dispatcher->setParams($params); 
     return $dispatcher; 
} 

이 코드는 전혀 작동하지 않는, 내 URL이 변경되지 않습니다. url은 mywebsite.com/이 며 mywebsite.com/en/이 아닙니다. 미리 감사드립니다.

위의 해결책을 하나 시도해 보겠습니다. 리디렉션이 작동하지 않는 것 같습니다. 심지어 테스트 용으로 하드 코딩하려고합니다.

use Phalcon\Http\Response; 

//Obtain the standard eventsManager from the DI 
$eventsManager = $di->getShared('eventsManager'); 
$dispatcher = new Phalcon\Mvc\Dispatcher(); 

$eventsManager->attach("dispatch:beforeDispatchLoop",function($event, $dispatcher) 
{ 
    $dispatcher->getDI->get('response')->redirect('/name/en/index/index/'); 
} 
+0

"내 URL은 변경되지 않았습니다."는 의미를 분명히합니다. – Kris

+0

내 질문을 편집합니다. 내가 설정 한 언어 매개 변수에 따라 url mywebsite.com/을 다른 것으로 변경하려고합니다. 언어 매개 변수를 'ru'로 설정하면 my urb는 mywebsite.com/ru가되어야합니다. – Biscottex

답변

0

나는 당신이 혼란 스럽다고 생각합니다. 올바르게 이해하면 언어를 지정하지 않고 페이지를 열면 사용자를 유효한 URL로 리디렉션하고 싶습니다.

그런 경우 이벤트 처리기에서 language 매개 변수가 지정되어 있는지 확인하고 누락 된 경우 사용자를 동일한 url + 기본 언어로 리디렉션해야합니다. 또한 귀하의 beforeDispatchLoop이 문제가되는 컨트롤러에 있다고 가정합니다. 언어가없는 경로가 결코 일치하지 않으며 해당 컨트롤러에 들어 가지 않기 때문입니다. 대신 documentation에 따라 이벤트 관리자와 함께 이벤트 처리기로 사용해야합니다. 여기 모든 것을하는 방법이 있습니다.

$di->get('eventsManager')->attach("dispatch:beforeDispatchLoop", function(Event $event, Dispatcher $dispatcher) 
{ 
    $params = $dispatcher->getParams(); 

    // Be careful here, if it matches a valid route without a language it may go into a massive 
    // recursion. Really, you probably want to use `beforeExecuteRoute` event or explicitly 
    // check if `$_SERVER['REQUEST_URI']` contains the language part before redirecting here. 

    if (!isset($params['language']) { 
     $dispatcher->getDI->get('response')->redirect('/en' . $_SERVER['REQUEST_URI']); 
     return false; 
    } 

    return $dispatcher; 
}); 
+0

리디렉션이 작동하지 않는 것 같습니다. 심지어 테스트 용으로 하드 코딩하려고합니다. 그러나 코드 구조가 더 좋을 것 같습니다. – Biscottex

+0

코드에서 주석별로 제안한 것처럼'beforeDispatchLoop'을 다른 것으로 변경하려고 시도 했습니까? –

+1

실수를 발견했습니다. $ this-> response-> redirect ('/ MyWebsite/cz/index/index', true); 더 잘 작동 왜 나 한테 물어 보지 마. 정말이 문제로 인해 고맙습니다. 이제 더 갈 수 있습니다. 또한 BeforeDispatchloop에 넣었습니다. – Biscottex