2011-04-27 5 views
1

"m.mydomain.com"하위 도메인에서 실행되는 모바일 페이지가 있습니다. 이것은 정상적으로 작동하지만 하위 도메인을 사용할 때 URL에서 컨트롤러를 제거하고 싶습니다. HTML 도우미로 CakePHP에서 하위 도메인 라우팅하기

m.mydomain.com/mobiles/tips 

는 HTML 도우미를 사용하여

m.mydomain.com/tips 

될 것이다.

는 순간 링크는 다음과 같습니다 나는 부트 스트랩의 경로와 일부 해킹 여러 해결 방법을 시도했지만 나를 위해 작동하지 않았다

$html->link('MyLink', array('controller' => 'mobiles', 'action'=> 'tips')); 

.

CakeBakery에서 this을 찾았지만 문제가 해결되지 않습니다.

누구든지이 문제에 대한 아이디어가 있습니까?

+0

내 대답을 볼 때마다 그것이 작동하지 않는다면 의견을 말하십시오. 그래서 우리는 확실한 대답을 향해 나아갈 수 있습니다. 이 질문은 많은 사람들에게 도움이 될 수 있습니다. – Oerd

답변

1

당신이 언급 한 페이지에서 코드를 수집 :

제약 : 당신의 컨트롤러 액션에서

$subdomain = substr(env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".")); 

if(strlen($subdomain)>0 && $subdomain != "m") { 
    Router::connect('/tips',array('controller'=>'mobiles','action'=>'tips')); 
    Router::connect('/foo', array('controller'=>'mobiles','action'=>'foo')); 
    Configure::write('Site.type', 'mobile'); 
} 

/* The following is available via default routes '/{:controller}/{:action}'*/ 
// Router::connect('/mobiles/tips', 
//     array('controller' => 'mobiles', 'action'=>'tips')); 
// Router::connect('/mobiles/foo', 
//     array('controller' => 'mobiles', 'action'=>'foo')); 

:이 설정에서 tips 또는 foo라는 컨트롤러

/config/routes.php에서 가질 수 없습니다 :

$site_is_mobile = Configure::read('Site.type') ?: ''; 
<?php 

if ($site_is_mobile) { 
    // $html will take care of the 'm.example.com' part 
    $html->link('Cool Tips', '/tips'); 
    $html->link('Hot Foo', '/foo'); 
} else { 
    // $html will just output 'www.example.com' in this case 
    $html->link('Cool Tips', '/mobiles/tips'); 
    $html->link('Hot Foo', '/mobiles/foo'); 
} 

?> 

이 당신의 뷰의 오른쪽 링크 (나는 방법을 보여 드리겠습니다 약간의도 적은 코드를 작성하는) 출력을 허용하지만 $html 도우미가되지 않습니다 : 그런 다음보기에서

다른 영역에 컨트롤러 - 액션 라우트를 사용할 수 있습니다. m.example.comwww.example.com$html 도우미와 관련된 다른 도메인입니다.

<?php 

$site_is_mobile = Configure::read('Site.type') ?: ''; 

if ($site_is_mobile !== '') { 
    $tips_url = '/tips'; 
    $foo_url = '/foo'; 
} else { 
    $tips_url = '/mobile/tips'; 
    $foo_url = '/mobile/foo'; 
} 

// make "urls" available to the View 
$this->set($tips_url); 
$this->set($foo_url); 

?> 

을 그리고보기에 당신은 사이트가되고 있는지 확인에 대해 걱정할 필요가 없습니다 :

지금, 당신이 원하는 경우에 당신은 당신의보기 떨어져 몇 가지 논리를 적용하려면 컨트롤러에서 다음을 수행 할 수 있습니다 m.example.com/tips 또는 www.example.com/mobile/tips를 통해 액세스 : Mark Story's article on custom Route classes

에 CakePHP는 - 1.3에서 고급 라우팅을 위해

<?php echo $html->link("Get some kewl tips", $tips_url); ?> 

참조저희에게 알려주십시오;)

+0

도움 주셔서 감사합니다. Oerd! 이 방법은 효과적이지만 www.mydomain.com/mobiles/tips 및 m.mydomain.com/tips와 같이 두 가지 방법으로 작동하는 것을 찾고있었습니다. html 도우미로이 작업을 수행 할 수 있습니까? – chris

+0

@chris 당신이 모빌 컨트롤러를 가지고 있다면'your_controller'가 실제로'mobiles_controller.php'라고 부름을 받았습니다. – Oerd

+0

@chris, 대답을 업데이트하고 있습니다 – Oerd