나는 항상 URI의 첫 번째 "항목"으로 사용자의 언어 설정이있는 웹 사이트 구조를 개발하고 PHP 5.5 Kohana 3.3Kohana 라우팅 (및 경로 변경) 문제
을 사용하고 있습니다. 예를 들어
:
mydomain.com/en/products
mydomain.com/de/store
지금, 내가 좋아하는 일부 사용자가 시도하고 영리하고 일을 입력 것이라고 확신합니다 :
괜찮mydomain.com/products
, 다시 전달하기를 바랍니다.
mydomain.com/en/products
모든 것을 일관되게 유지합니다.
아래 코드는 uri가 URI에 하나의 "디렉토리"만 가지고있는 한 작업하고 있습니다.
Route::set('home_page', '(<lang>)')
->defaults(array(
'controller' => 'Index'
));
Route::set('default', '(<lang>(/<controller>(/<action>(/<subfolder>))))')
->defaults(array(
'controller' => 'Index',
'action' => 'index'
));
: 여기
mydomain.com/store/purchase/info
mydomain.com/products/something
내 경로입니다
mydomain.com/store
하지만 URI의
mydomain.com/products
다른 모든 컨트롤러에서 상속 부모님 컨트롤러에 코드입니다 :
public function before()
{
$this->uri = $this->request->uri();
$this->lang = $this->request->param('lang');
//If user directly inputted url mydomain.com without language information, redirect them to language version of site
//TODO use cookie information to guess language preferences if possible
if(!isset($this->lang))
{
$this->redirect('en/', 302);
}
//If the first part of path does not match a language redirect to english version of uri
if(!in_array($this->lang, ContentManager::getSupportedLangs()))
{
$this->redirect('en/'.$this->uri, 302);
}
}
사용자가 URL (ko, fr 또는 pl)에 lang을 지정하지 않은 경우 404가 제공되지 않습니까? 나는'/ products/something'이라는 URL을'/ en/products/something'에 보내고 싶습니다. 사용자가 주소 표시 줄의 주소를 펀치하면 url에 언어를 지정해야한다는 사실을 사용자가 완전히 알기를 원하지 않습니다. – thatidiotguy
아니요 404를 전달하지 않습니다. 시도해보십시오! 그 이유는'( /)'의 괄호가 그 부분을 선택적으로 만들기 때문입니다. 따라서 지원되는 언어를 사용하여 일치하는 항목이없는 경우 해당 언어가없는 것처럼 처리합니다. –
Jonathan
물론 당신은 옳았습니다. 죄송합니다. 지금 방금 시도해보고 있습니다. 조언 해 주셔서 대단히 감사드립니다. – thatidiotguy