Kohana:未找到请求的 URL

发布于 2024-12-29 03:25:59 字数 2443 浏览 1 评论 0原文

所以我正在尝试安装并运行 Kohana。我对它和一般框架都很陌生(尽管我使用过一点 CakePHP)。

无论如何......在我的引导文件中我有这个:

// GET PARAMS -- This basically splits domain.com/kohana/controller/action/param1/etc
// into: controller | action | param1 | etc
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);

    for($i= 0;$i < sizeof($scriptName);$i++){
      if ($requestURI[$i] == $scriptName[$i]){
         unset($requestURI[$i]);
      }
    }

    $param = array_values($requestURI);
    $controller = @ $param[0];
    $action = @ $param[1];
    $param1 = @ $param[2];
    $param2 = @ $param[3];
    $param3 = @ $param[4];
    $param4 = @ $param[5];
    $param5 = @ $param[6];
    $param6 = @ $param[7];

现在,我想连接到我的数据库,基本上看看第一个参数(又名 $param[0] 又名 $controller)是否等于我的页面“类别”之一(对应于我的categories_pages 表)。如果它是一个类别,那么我想使用默认的“类别”控制器,否则控制器应该是 $param[0] 是什么。这意味着如果我转到domain.com/kohana/movies 或domain.com/kohana/games 它将显示类别控制器,否则domain.com/kohana/users 将显示用户控制器。

$db = Database::instance();
$getiscategory  = DB::select('*')->from('categories_pages')->where('directory', '=', $controller)->execute();
$is_category = $getiscategory->count();

if($is_category){
$controller = "categories";
}

    $controller = (empty($controller)) ? 'index' : $controller;
    $action = (empty($action)) ? 'index' : $action;      

我想完成上述工作。如果我 echo $is_category ,我会看到当类别匹配时返回值 1;如果我 echo $controller ,我会看到“categories”被设置为控制器。

这是我的实际路由方法的代码...

Route::set(
    'custom',
    '(<controller>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>)))))))'
    )->defaults(array(
    'controller' => $controller,
    'action'     => $action,
    'param1'     => $param1,
    'param2'     => $param2,
    'param3'     => $param3,
    'param4'     => $param4,
    'param5'     => $param5,
    'param6'     => $param6,
));

不幸的是,我不确定它路由到哪里。正如我提到的,$controller 之前作为类别返回,这是正确的,但我收到错误消息“HTTP_Exception_404 [404]:在此服务器上找不到请求的 URL $param[0](电影或游戏等)。”

请记住,如果 $param[0] 与类别匹配,我没有控制器类,因为我想使用“类别”控制器类。如果我访问domain.com/kohana/categories,它工作正常。

有人有任何想法/知道解决方法吗?

添加#1

我发现即使类别控制器被调用并且正确的操作被调用,它仍然需要“显示”控制器来显示。我添加了一个显示控制器,其中包含非常基本的信息(模板、内容等),并且它显示正确。路由类中是否有解决方法可以使指定的控制器显示?就像我说的,我告诉它要转到哪个控制器,它会承认它,但实际上并没有转到它。

So I'm trying to install and get running Kohana. I am very new to it and frameworks in general (though I have used CakePHP a little bit).

Anyways...in my bootstrap file I have this:

// GET PARAMS -- This basically splits domain.com/kohana/controller/action/param1/etc
// into: controller | action | param1 | etc
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/',$_SERVER['SCRIPT_NAME']);

    for($i= 0;$i < sizeof($scriptName);$i++){
      if ($requestURI[$i] == $scriptName[$i]){
         unset($requestURI[$i]);
      }
    }

    $param = array_values($requestURI);
    $controller = @ $param[0];
    $action = @ $param[1];
    $param1 = @ $param[2];
    $param2 = @ $param[3];
    $param3 = @ $param[4];
    $param4 = @ $param[5];
    $param5 = @ $param[6];
    $param6 = @ $param[7];

Now, I want to connect to my database and basically see if the first param aka $param[0] aka $controller is equal to one of my pages "categories" (corresponds to my categories_pages table). If it IS a category then I want to use the default "categories" controller otherwise, the controller should be whatever $param[0] is. This means if I go to domain.com/kohana/movies or domain.com/kohana/games it will display the categories controller otherwise domain.com/kohana/users will display the users controller.

$db = Database::instance();
$getiscategory  = DB::select('*')->from('categories_pages')->where('directory', '=', $controller)->execute();
$is_category = $getiscategory->count();

if($is_category){
$controller = "categories";
}

    $controller = (empty($controller)) ? 'index' : $controller;
    $action = (empty($action)) ? 'index' : $action;      

What I wanted to accomplish above works. If I echo $is_category I see that the value 1 is returned when there is a category match and if I echo $controller, I see that "categories" is set to be the controller.

Here is my code for the actual routing method...

Route::set(
    'custom',
    '(<controller>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>)))))))'
    )->defaults(array(
    'controller' => $controller,
    'action'     => $action,
    'param1'     => $param1,
    'param2'     => $param2,
    'param3'     => $param3,
    'param4'     => $param4,
    'param5'     => $param5,
    'param6'     => $param6,
));

Unfortunately, I'm not sure where it's routing to. As I mentioned $controller is returned previously as categories which is correct but yet I receive the error message "HTTP_Exception_404 [404]: The Requested URL $param[0] (movies or games, etc) was not found on this server."

Keep in mind I do not have a controller class for $param[0] if it matches a category because I want to use the "categories" controller class. If I go to domain.com/kohana/categories it works fine.

Anyone have any ideas/know a work-around?

Addition #1

I figured out that even though the categories controller is called and the correct action is called, it still is requiring the "shows" controller to display. I added a shows controller with the very basic info (template, content, etc) and it showed correctly. Is there a work around in the routing class to make the designated controller show? Like I said, I tell it what controller to go to and it acknowledges it but it doesn't actually go to it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2025-01-05 03:25:59

我认为你把事情过于复杂化了。此外,您的代码会强制每个请求限制 6 个参数。

我可能会更容易使用两条不同的路线,如果它与第一条路线不匹配,那么它可以退回到第二条路线。

Route::set(
    'categories',
    '(<category>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>)))))))',
    array('category' => '(movies|games)')
    )->defaults(array(
        'controller' => 'category',
        'action'     => 'index',
));
Route::set(
    'users',
    '<username>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>))))))'
    )->defaults(array(
        'controller' => 'users',
        'action'     => 'index',
));

如果您只有一两个类别,您可以将它们构建到正则表达式字符串中,当然还需要一些缓存。

否则,查找 lambda 路由,它们是通过你可以拥有像这样的动态路线。

You're overcomplicating things I think. Also your code would force a limit of 6 parameters on every request.

I'll probably be easier to use two different routes, and if it doesn't match the first, then it can fall back to the second.

Route::set(
    'categories',
    '(<category>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>)))))))',
    array('category' => '(movies|games)')
    )->defaults(array(
        'controller' => 'category',
        'action'     => 'index',
));
Route::set(
    'users',
    '<username>(/<action>(/<param1>)(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>))))))'
    )->defaults(array(
        'controller' => 'users',
        'action'     => 'index',
));

If you only have one or two categories you could build them into the regex string, with some caching of course.

Otherwise, look up lambda routes, they're the means by which you can have dynamic routes like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文