Gulmuhammad Akbari
Gulmuhammad Akbari

Reputation: 2036

Laravel redirect to another route from route file

I have the following routes in my web.php route file:

Route::get('/contacts', 'ContactController@getAll')->name('getContacts');
Route::get('/contactsData', 'ContactController@getData')->name('getContactData');

And now I want to redirect to the above route name with the following condition:

$host = request()->getHttpHost();
if($host=="example.com")
{  
    return redirect()->route('getContacts');
}

But I got the following errors:

Route [getContacts] not defined.

I have tried the following as well:

if($host=="example.com")
{  
   return redirect()->action('ContactController@getAll');
}

Got the following error:

Class ContactController not defined.

Upvotes: 0

Views: 351

Answers (1)

Bashir Noori
Bashir Noori

Reputation: 679

You can check the current URL on the view and then redirect. For example:

@if(Request::url() === 'http://example.com')
    <script>window.location = "http://example.com/contacts";</script>
@endif

Upvotes: 1

Related Questions