简体   繁体   中英

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. Problem: same url structure for multiple different database lookups. 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 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. Use closure and call appropriate controller after examining url segments?

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?

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. 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();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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