A
A
Artem Dolgikh2017-01-22 21:01:45
Computer networks
Artem Dolgikh, 2017-01-22 21:01:45

How to set dynamic number of parameters in URI (Routes) laravel?

Let's say the site has a list of products, I need to configure their filtering.
For example, the output of goods by city, by category or by manufacturer. I can process each one individually, but what if I need to make a selection by city and category? Or by city and manufacturer?
I'm trying to do this (code in Routes.php):

Route::group(['prefix' => '/{city_name?}/{category_name?}/{firm_name?}/'], function() {
            Route::get('/', '[email protected]');
        });

In the controller I accept these values:
public function index($city_name=0,$category_name=0,$firm_name=0){}

If I have all 3 parameters, then everything is fine, but if one is missing, then all parameters are shifted and nothing works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
codeschemer, 2017-01-23
@Dolgitem

Your routes could look something like this
and your controller is something like this

public function index(Request $request)
  {
    $cityName = '';
    $categoryName= '';
    $firmName= '';
    if(isset($request->city_name) || isset($request->category_name) || isset($request->firm_name)){
      //POST случай делаешь свои выборки, получаешь $списокТоваров
      }
       if(!isset($списокТоваров)){
      //GET случай, когда первый раз зашёл на страницу.
      }
      return view('/', compact('списокТоваров', 'cityName ', 'categoryName', 'firmName'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question