【问题标题】:Laravel 5 - route typesLaravel 5 - 路线类型
【发布时间】:2016-08-04 11:49:01
【问题描述】:

想不出更具描述性的标题。

这是两难境地,我想知道是否有人对此有好的方法,或者我是否忽略了一些明显的东西?

想象一下这两条路线:

/category/product
/parent/child

第一条路线处理商店中的产品。 第二条路线处理 CMS 中的常规页面。

如何在我的应用程序中区分这两种路由?如果可能的话,我不想在每条路线之前附加一些东西(即/shop/category/product、/page/parent/child)。

有什么想法吗?

【问题讨论】:

    标签: php laravel routing


    【解决方案1】:

    在你的 routes.php 文件中输入这段代码

     Route::get('/{action?}/{name?}',[
            'uses' =>'NiceActionController@getNiceAction',
            'as' =>'niceaction'
        ]);
    

    然后转到控制器文件夹并创建一个文件 NiceActionController 然后:

     <?php
    namespace App\Http\Controllers;
    use \Illuminate\Http\Request;
    class NiceActionController extends Controller
    {
        public function getNiceAction($action,$name=null)
        {
            return view('actions.'.$action , ['name' => $name]) ;
        }
    }
    

    【讨论】:

    • 您必须在此处的 URL 中提供操作,这不是我需要的。通过 /category/product - category 将是实际的类别名称,product 将是实际的产品名称。这有意义吗?
    • 只需您使用操作文本更改产品
    【解决方案2】:

    应该可以只堆叠路线:

    Route::get('category/{product}', 'Controller@function');
    Route::get('{parent}/{child}', 'Controller@function');
    

    【讨论】:

      【解决方案3】:

      您可以对路线进行分组。

      Route::group(['prefix' => 'parent'], function () {
       Route::get('child', 'Controller1@action1');
       Route::get('anotherchild', 'Controller2@action2');
      });
      

      这将为您提供/parent/child/parent/anotherchild 的路线。

      还有更多选择,请在documentation阅读更多内容

      【讨论】:

        猜你喜欢
        • 2015-12-11
        • 2016-08-24
        • 1970-01-01
        • 2018-05-15
        • 1970-01-01
        • 2015-12-24
        • 2016-07-03
        • 2017-10-11
        • 2015-11-30
        相关资源
        最近更新 更多