How to create multiple parameters dynamic routes in laravel?

Sometimes, we needs to add multiple routes in our laravel application. Here we will create dynamic routes so we can use as many parameters.

First of all add the code given below in your routes file. we are using the laravel 5.5 so our routes file is w…


This content originally appeared on DEV Community and was authored by Chetan Rohilla

Sometimes, we needs to add multiple routes in our laravel application. Here we will create dynamic routes so we can use as many parameters.

First of all add the code given below in your routes file. we are using the laravel 5.5 so our routes file is web.php under routes directory.

Route::get('{slug?}', 'UriController')->name('page_url')->where('slug','.+');

Now create a controller file UriController.php under your controllers directory and add the code given below.

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;

class UriController extends Controller
{
    /**
     * Show the page for the given slug.
     *
     * @param  string  $request uri
     * @return Response
     */
    public function __invoke(Request $request)
    {

        $request_path = $request->path();

        $request_path_array = explode('/', $request_path);

    }


}

Code Explanation:

We add a parameter {slug?} in our route and added the where condition. So, we can add as many parameters dynamically. You can also add any regular expression in where condition. Now your url will look like this https://example.com/parameter1/parameter2 and so on.

We have used the name method in our routes to access routes through name in our blade templates or controllers like this

route('page_url',['slug' => 'abc/123']);

Here we created a Single Action Controller UriController and we gets the uri in our __invoke method.

In our request method we have $request_path_array. So, we can loop through our dynamic parameters passed in url.

If you want do not want to apply particular uri to the route above then you can use the code given below which uses the all routes except the api in the routes.

Route::get('{slug?}', 'UriController')->name('page_url')->where('slug','^(?!api).*$');

If you want add prefix to your routes then you can use the code given below. Now your url will look like this https://example.com/your-prefix/parameter1/parameter2 and so on.

Route::prefix('your-prefix')->group(function () {

    Route::get('{slug?}', 'UriController')->name('page_url')->where('slug','.+');

});

Thanks:)
Please give your suggestions :)


This content originally appeared on DEV Community and was authored by Chetan Rohilla


Print Share Comment Cite Upload Translate Updates
APA

Chetan Rohilla | Sciencx (2021-08-25T21:56:27+00:00) How to create multiple parameters dynamic routes in laravel?. Retrieved from https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/

MLA
" » How to create multiple parameters dynamic routes in laravel?." Chetan Rohilla | Sciencx - Wednesday August 25, 2021, https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/
HARVARD
Chetan Rohilla | Sciencx Wednesday August 25, 2021 » How to create multiple parameters dynamic routes in laravel?., viewed ,<https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/>
VANCOUVER
Chetan Rohilla | Sciencx - » How to create multiple parameters dynamic routes in laravel?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/
CHICAGO
" » How to create multiple parameters dynamic routes in laravel?." Chetan Rohilla | Sciencx - Accessed . https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/
IEEE
" » How to create multiple parameters dynamic routes in laravel?." Chetan Rohilla | Sciencx [Online]. Available: https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/. [Accessed: ]
rf:citation
» How to create multiple parameters dynamic routes in laravel? | Chetan Rohilla | Sciencx | https://www.scien.cx/2021/08/25/how-to-create-multiple-parameters-dynamic-routes-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.