简体   繁体   English

Laravel 路由现实生活用例

[英]Laravel routing real life use case

I am posting Laravel 5.2 real life routing use case and would like to have an answer for it.我正在发布 Laravel 5.2 现实生活中的路由用例,并希望得到一个答案。 Problem: same url structure for multiple different database lookups.问题:多个不同数据库查找的 url 结构相同。 Please do not post remarks on how to make easier URL structure, this is the way the structure must be and many sites use it in this segment.请不要发表关于如何简化 URL 结构的评论,这是结构必须采用的方式,许多站点在此部分中使用它。

URL structure网址结构

domain.com/{slug1}/{slug2}/{slug3} 
// e.g. domain.com/cottages/slovakia/cheap
// {slug1} - DB table accommodation_types (20+)
// {slug2} - DB table locations (300+) 
// {slug3} - DB table accommodation_categories e.g. cheap etc. (100+)

domain.com/{slug1}/{slug2} 
// e.g. domain.com/cottages/cheap OR domain.com/slovakia/cheap
// {slug1} - DB table accommodation_types OR locations  
// {slug2} - DB table locations OR accommodation_categories 

domain.com/{slug}  
// DB table accommodation (10000+ entries)
// or
// accommodation_types OR locations OR accommodation_categories 

How would you do it nicely?你会怎么做? I have these ideas.我有这些想法。

a. a. Use closure and call appropriate controller after examining url segments?在检查 url 段后使用闭包并调用适当的控制器?

Route::get('{slug1}', function ($slug1, $slug2 = null, $slug3 = null)
{
    // Check accommodation
    $object = Accommodation::where('slug', $slug1)->first();

    if( !is_null($object) )
    {
        return app()->make('AccommodationDetailController')->view($object);
    }

    // Check type
    $type = AccommodationType::where('slug', $slug1)->first();

    if( !is_null($type) )
    {
        return app()->make('AccommodationListController')->view($type);
    }

    // etc.
});

b.Generate thousands of urls by for loop and cache it then?通过 for 循环生成数千个 url 然后缓存它?

I appreciate any other great solution我很欣赏任何其他伟大的解决方案

I think the best way is to send all these routes to the same controller action and edit your query according to the parameters that are sent.我认为最好的方法是将所有这些路由发送到同一个控制器操作并根据发送的参数编辑您的查询。

For example, this would be your routing file:例如,这将是您的路由文件:

<?php

Route::get('{slug1}', 'Controller@getPage');
Route::get('{slug1}/{slug2}', 'Controller@getPage');
Route::get('{slug1}/{slug2}/{slug3}', 'Controller@getPage');

In the controller, you can use Eloquent or the query builder to build the sql query according to the variables you received from the route.在控制器中,您可以使用 Eloquent 或查询构建器根据您从路由中收到的变量来构建 sql 查询。 Below is a simple example:下面是一个简单的例子:

<?php

class Controler {

    public function getPage($slug1, $slug2 = null, $slug3 = null) {

        $models = Model::where(function ($query) use ($slug1) {
            $query->where('accomodation_types', $slug1)
                ->orWhere('location', $slug1);
        })
        ->where(function ($query) use ($slug2) {
            if (isset($slug2)) {
                // Slug2 query
            }
        })
        ->where(function ($query) use ($slug3) {
            if (isset($slug3)) {
                // Slug3 query
            }
        })
        ->get();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM