的Kohana 3.1.1路由问题jQuery的标签

问题描述:

为了问我的问题,我需要解释一下我的代码第一...的Kohana 3.1.1路由问题jQuery的标签

我有一个控制器(Controller_App)延伸Controller_Template。在控制器的模板视图中,我有3个选项卡的jQuery选项卡。当我访问URI:/view/26,以如下的路线踢:

Route::set('view_story', '<action>/<id>(/<stuff>)', array(
    'action' => 'view', 
    'id'  => '\d+', 
    'stuff'  => '.*', 
)) 
->defaults(array(
    'controller' => 'app', 
)); 

下面的函数,然后调用在Controller_App和设置的的URI“探索” jQuery的标签,并使其成为默认的选择:

public function action_view($id) 
    { 
     $this->template->controller['explore'] = Route::get('explore') 
      ->uri(array(
       'controller' => 'explore', 
       'id'   => $id, 
      )); 
     $this->template->default_tab = 2; 
    } 

这里是我的 “探索” 路线:

Route::set('explore', '<controller>/<id>', array(
    'controller' => 'explore', 
    'id'   => '\d+', 
)) 
->defaults(array(
    'action' => 'index', 
)); 


问题:
当我尝试访问URL为“myhost.com/view/26”的故事时,它设置了一切正常,但它认为“/ view”是一个目录,因此它会尝试调用“ myhost.com/view/explore/26。由于没有控制器称为“视图”,我得到一个404错误。我能得到周围的404错误通过创建以下路线:

Route::set('explore', '(<directory>/)<controller>/<id>', array(
    'directory'  => 'view', 
    'controller' => 'explore', 
    'id'   => '\d+', 
)) 
->defaults(array(
    'directory' => '', 
    'action' => 'index', 
)); 

...然后改变我的功能:

public function action_view($id) 
    { 
     $this->template->controller['explore'] = Route::get('explore') 
      ->uri(array(
       'directory' => '', 
       'controller' => 'explore', 
       'action'  => 'index', 
       'id'   => $id, 
      )); 
     $this->template->default_tab = 2; 
    } 

但加载页面时,它调用jQuery.get (),但它试图调用“/ view”目录下的PHP文件而不是当前目录。

我不知道这是一个简单的路由问题,或者如果我什至吠叫了正确的树。但我已经尝试了所有不同的路线组合,并且不能为我的生活弄清楚这一点。所有的建议表示赞赏!

感谢, 布赖恩

+0

你说当你导航到'myhost.com/view/26'时,一切正常,但它会尝试调用'myhost.com/view/explore/26'?什么试图调用'myhost.com/view/explore/26'?也许你需要在路由输出中包装'URL :: site()'? – 2011-05-06 10:22:28

+0

@davgothic - 对不起,昨天晚上我发布这个时候已经很晚了。 ;)在action_view函数中(第一个函数),它为探索控制器构建一个路由。但是当它做到这一点时,它会保留'/ view'目录并将其放在我的路线上。如果我修改了路由定义并修改了我的'action_view'函数(如第二个例子所示),它解决了这个问题,但是我的AJAX调用混乱了,因为它不是调用'core/stories',而是调用'/ view /核心/ stories'。这就像我需要改变我目前的工作直接上一级... – DondeEstaMiCulo 2011-05-06 20:02:51

Route::uri(...)产生的URI这不是绝对的。 切换到使用Route::url(...),你应该很好去。

Route::url(...)是通过Route::uri(...)URL::site(...)的快捷方式。

public function action_view($id) 
{ 
    $this->template->controller['explore'] = Route::get('explore') 
     ->url(array(
      'controller' => 'explore', 
      'id'   => $id, 
     )); 
    $this->template->default_tab = 2; 
} 
+0

问题是当从我的外部JS文件内进行AJAX调用。我会在哪里放置Route/URL方法? – DondeEstaMiCulo 2011-05-14 01:18:04

+0

我的预感是AJAX代码已经被赋予了一个相对的URI,而不是绝对的(即'foo.html'而不是'/ foo.html'),因此代码试图从当前的代码中找到一个dir网址,然后添加相对的网址。在你做这个任务的地方改变它:'$ this-> template-> controller ['explore'] = ..' – Lethargy 2011-05-15 21:39:42