草庐IT

拉维尔 : create hierarchical route for category

coder 2024-02-27 原文

我正在实现类别结构,有些产品会有一级类别,但其他产品可能有两级或更多级别:

/posts/cat2/post-sulg

/posts/cat-1/sub-1/post-slug

/posts/cat-3/sub../../../post-slug

因为我不知道它会有多深并且使用类别 slugs 仅适用于 seo(我只通过它的 slug 找到帖子)创建处理此结构的路由的最佳方法是什么?

最佳答案

你可以解决这个问题:

Route::get('posts/{categories}', 'PostController@categories')
    ->where('categories','^[a-zA-Z0-9-_\/]+$');

然后在 Controller 中

class PostController
{
    public function categories($categories)
    {
        $categories = explode('/', $categories);
        $postSlug = array_pop($categories)

        // here you can manage the categories in $categories array and slug of the post in $postSlug
        (...)
    }

}

关于拉维尔 : create hierarchical route for category,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40716985/

有关拉维尔 : create hierarchical route for category的更多相关文章

  1. php - 如何在 foreach 迁移表中使用计数器? (拉拉维尔 5.3) - 2

    我的代码是这样的:$value){$masterLookup=newMaster_lookup;$masterLookup->id=++$key$masterLookup->parent_id=NULL;$masterLookup->code=$value->kdakun;$masterLookup->name=$value->nmakun;$masterLookup->type='akun';$masterLookup->information=json_encode($value->kdjenbel);$masterLookup->save();}}}我用索引$key来反击但是执行

  2. php - 如何将此菜单包含在我的所有 View 中? [拉维尔 5] - 2

    你好,我正在尝试从我的table创建一个菜单,然后我想将它分享到我的View。这是我到目前为止的尝试namespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Input;useApp\Menuasmenumodel;classMaincontrollerextendsController{//publicfunction__construct(){$items=menumodel::tree();View::make('items',$items);}}然后在我的Cont

  3. php - 拉维尔 : Class 'App\DB' not found - 2

    我正在尝试使用DB类来运行查询。我在我的Controller中使用它是这样的:use\DB;我在我的代码中使用它是这样的:$changeCar=DB::select(DB::raw($query));我试过像这样包含它:useIlluminate\Support\Facades\DB但还是一样。另外,尝试在代码中使用反斜杠,而不是像这样包含它:$changeCar=\DB::select(\DB::raw($query));我的代码:namespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Suppor

  4. php - 拉维尔 5.5 : Can't sent email via google (SSL operation failed) - 2

    这个问题在这里已经有了答案:LaravelcertificateverificationerrorswhensendingTLSemail(2个答案)关闭去年。我在类似问题的答案中看到了.env的以下设置:MAIL_DRIVER=smtpMAIL_HOST=smtp.gmail.comMAIL_PORT=587MAIL_USERNAME=myemail@gmail.comMAIL_PASSWORD=somePassword1234MAIL_ENCRYPTION=tls然而,没有任何效果。使用tls加密,我得到了错误stream_socket_enable_crypto():SSLope

  5. php - 拉维尔 5.4 : get products with favorite and display favorites product for each user - 2

    我正在尝试获取收藏夹中的所有产品并像用户登录一样显示它们,收藏夹中的该产品将显示为收藏夹,否则他可以将它们添加到收藏夹。这是我的Controller$products=(newProduct)->where('quantity','>',0)->with('favorite')->orderBy('price',$sort)->get();现在如果我制作dd($product->favorite)我会像这样得到最喜欢的数组[{"id":1,"product_id":7,"user_id":1,"created_at":"2018-04-0109:16:23","updated_at":

  6. php - 拉维/Javascript : populate a select/dropdown after a different select/dropdown is selected - 2

    我正在寻找一种解决方案,以便在从另一个下拉列表(例如下拉列表1)中选择一个选项时填充下拉菜单/选择(例如称为下拉列表2)。例如,从下拉列表1中选择Toyota将在下拉列表2中填充丰田的型号,例如卡罗拉、凯美瑞等。或者另一个例子——在一个下拉列表中选择一个国家将在另一个下拉列表中填充该国家的不同城市。我使用laravel作为我的框架,所以重要的是我的解决方案(a)在laravel中工作并且(b)从mysql数据库中获取结果,而不仅仅是一个硬编码的值数组。我已尝试从此处的帖子中实现解决方案:http://forums.laravel.io/viewtopic.php?pid=40028但它

  7. php - 拉维尔 4 : Model Relationships Not Working (sort of)? - 2

    我的Laravel4项目中有3个模型:Employee、EmployeeClass、Employer:classEmployeeextendsEloquent{protected$table='users';publicfunctionemployee_class(){return$this->belongsTo('EmployeeClass','employee_class_id');}}classEmployeeClassextendsEloquent{protected$table='employee_classes';publicfunctionemployees(){retu

  8. php - 拉维尔 5 : How to retrieve records according to date? - 2

    我有一个名为entries的表,它的created_at被定义为DATE类型。例如,如何获取3月15日的记录?我尝试过的$post=Entry::where(\DB::raw('DATE(created_at)'),'=',date('2015-03-15'))->get()->toJSON();但它返回空结果。迁移Schema::create('entries',function(Blueprint$table){$table->increments('id');$table->integer('intern_id');$table->integer('company_id');$t

  9. php - 拉维尔 5 : build URL with custom parameters - 2

    我有一个应用程序,您可以在其中选择多个客户。选择客户将生成以下URL:http://localhost:8000/customer/CUSTOMER_NAME从那里,我想选择一个特定的子页面(例如:支持页面)如何生成以下链接:http://localhost:8000/customer/CUSTOMER_NAME/support到目前为止,我总是丢失我的CUSTOMER_NAME参数,而且我不知道如何保留它。我使用的框架是Laravel5。有什么想法吗? 最佳答案 你应该通过将url参数传递给View来做到这一点我相信你的route

  10. php - 拉维尔 5 : No hint path defined for [Auth] - 2

    我正在加载邮件View$this->mail->send('auth::emails.email_confirmation',['url'=>$confirmationEmailUrl],function($m)use($user,$toMail){$m->to($toMail,$user->name)->subject('mail');});和服务提供商publicfunctionregisterViews(){$viewPath=base_path('resources/views/modules/auth');$sourcePath=__DIR__.'/../Resources/

随机推荐