在Kohana 3中路由ajax请求?

时间:2022-06-02 18:48:51

In my bootstrap.php I have the following code:

在我的bootstrap.php中,我有以下代码:

// Check if ajax request
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
{
    Route::set('ajax', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));
}

Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

The 'ajax' route is incomplete. What I want to do is if the request is done via. ajax, then Kohana should look for the controller in a subfolder within my controllers called ajax/. So, if ajax request then:

'ajax'路线不完整。我想要做的是如果请求是通过。 ajax,然后Kohana应该在我的控制器中的子文件夹中查找名为ajax /的控制器。那么,如果ajax请求那么:

http://localhost/myproject/somecontroller/someaction routes to somecontroller inside the ajax subfolder. If not ajax then just use the 'default' route.

http:// localhost / myproject / somecontroller / someaction路由到ajax子文件夹中的somecontroller。如果不是ajax,那么只需使用'默认'路线。

2 个解决方案

#1


2  

Use a lambda/callback function/method something like this:

使用像这样的lambda / callback函数/方法:

Route::set('ajax', function($uri)
    {
        if (Request::$current->is_ajax() AND $params = Route::get('default')->matches($uri))
        {
            $params['directory'] = 'ajax';
            return $params;
        }
    },
    '(<controller>(/<action>(/<id>)))'
);

See http://kohanaframework.org/3.2/guide/kohana/routing and http://kohanaframework.org/3.2/guide/api/Route

见http://kohanaframework.org/3.2/guide/kohana/routing和http://kohanaframework.org/3.2/guide/api/Route

#2


1  

Try this:

尝试这个:

Route::set('ajax', '(<controller>(/<action>(/<id>)))')
->defaults(array(
    'directory'  => 'ajax',
    'controller' => 'home',
    'action'     => 'index',
));

However, personally, I'd handle both AJAX and non-AJAX requests in the same controller, using Request::current()->$is_ajax to tell if it was an AJAX request. The AJAX behavior is probably not significantly different to the non-AJAX, so it might be beneficial to keep both in the same controller. You may end up with code duplication if you handle AJAX requests in a different controller.

但是,就个人而言,我会在同一个控制器中处理AJAX和非AJAX请求,使用Request :: current() - > $ is_ajax来判断它是否是一个AJAX请求。 AJAX行为可能与非AJAX没有显着差异,因此将两者保持在同一个控制器中可能是有益的。如果您在不同的控制器中处理AJAX请求,则最终可能会出现代码重复。

#1


2  

Use a lambda/callback function/method something like this:

使用像这样的lambda / callback函数/方法:

Route::set('ajax', function($uri)
    {
        if (Request::$current->is_ajax() AND $params = Route::get('default')->matches($uri))
        {
            $params['directory'] = 'ajax';
            return $params;
        }
    },
    '(<controller>(/<action>(/<id>)))'
);

See http://kohanaframework.org/3.2/guide/kohana/routing and http://kohanaframework.org/3.2/guide/api/Route

见http://kohanaframework.org/3.2/guide/kohana/routing和http://kohanaframework.org/3.2/guide/api/Route

#2


1  

Try this:

尝试这个:

Route::set('ajax', '(<controller>(/<action>(/<id>)))')
->defaults(array(
    'directory'  => 'ajax',
    'controller' => 'home',
    'action'     => 'index',
));

However, personally, I'd handle both AJAX and non-AJAX requests in the same controller, using Request::current()->$is_ajax to tell if it was an AJAX request. The AJAX behavior is probably not significantly different to the non-AJAX, so it might be beneficial to keep both in the same controller. You may end up with code duplication if you handle AJAX requests in a different controller.

但是,就个人而言,我会在同一个控制器中处理AJAX和非AJAX请求,使用Request :: current() - > $ is_ajax来判断它是否是一个AJAX请求。 AJAX行为可能与非AJAX没有显着差异,因此将两者保持在同一个控制器中可能是有益的。如果您在不同的控制器中处理AJAX请求,则最终可能会出现代码重复。