【问题标题】:Laravel 4 self relationship nested routesLaravel 4自我关系嵌套路由
【发布时间】:2014-11-06 18:54:12
【问题描述】:

我从 laravel 开始,我需要做这样的事情 localhost/diretory1/directory2/directory3

是否可以像嵌套路由一样设置它?目前它的工作方式是这样的 localhost/directory1 -> localhost/directory2

【问题讨论】:

  • 目录名应该是动态的还是静态的?
  • 它们是动态的,用户可以随意命名。
  • 好的。是最多 3 个级别还是有无限个级别?
  • 理论上4个是最大值,但我不希望以后有问题所以我想我不得不说它和无限数量的级别
  • 好的。给我一点时间,我会在今天晚些时候写一个答案......

标签: laravel laravel-4 routing nested-resources


【解决方案1】:

因此,您可以做的是定义一个捕获所有请求的 slug 路由。 (确保您在该路由之上定义了其他路由,因此如果没有其他匹配,请求只会在 slug 路由中结束)

Route::any('{slug}', function($slug){
    $directories = explode('/', $slug);

    // lookup the directory(ies) in the db, file system, etc 

    if(!$exists){
        // when the directories don't exists, it's probably appropriate to throw a 404 Not found error.
        App::abort(404);
    }
}

您在路由函数(或控制器,如果它在 routes.php 中获得太多代码)做什么取决于您。我不知道您的应用程序是如何工作的,所以我无法为您提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 2014-10-27
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多