skip to Main Content

I am newbie to laravel and i am working on a project and i have a following situation

lets assume my base url is https://example.com

Now i want to pass a slug(argument) after a base url which means https://example.com/xyz something like that, and i need to do this on multiple times in my project

This is what i’d tried but it is not working it says that route is not defined.

Route::get('{slug?}', [AppHttpControllersUiviewsController::class, 'method1'])->name('method1');

Route::get('/method2/{slug?}', function($slug){
    return redirect()->route('method1', ['slug'=>$slug]);
});

And also how can i achieve that on which argument which particular method should be called? for example if i have several other routes similar to above one.

how can i achieve this?

Thank you in advance for your help. 🙂

2

Answers


  1. You should use the fallback system.

    Route::fallback(function () {
        //
    });
    

    Laravel Route fallback official docs

    also, beware:

    The fallback route should always be the last route registered by your
    application.

    Other Option:

    Also, you can define a parameter as below example

    Route::any('{any}', function(){
        //...
    })->where('any', '.*');
    
    Login or Signup to reply.
  2. Please Try php artisan route:cache in your terminal then check it again.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search