DEBASHIS BAIDYA
DEBASHIS BAIDYA

Reputation: 320

How to I redirect to this URL in laravel?

Here is defined some of my routes:

Route::get('/success', function () {
     return view('dashboard/dashboard');

  });

 Route::get('/test/{id?}','TestController@parameter' );

 Route::get('/redd','TestController@redirectest' );

And here is the controller Method :

public function redirectest(){

   return redirect()->route('dashboard.dashboard');
  }

Now I have two view named

       test.blade.php
       dashboard.blade.php  //In view/dashboard

Here is the test.blade.php view just have only a link to redirect to dashboard by controller method.

 <html>

 <body>
    <h2><a href="/lvel/public/redd/">Click Me To Redirect to DashBoard</a> 
    </h2>

 </body>
 </html>

Now I want to do is simply redirect to the dashboard.blade.php when click on the view link in test.blade.php

But I am getting

      Route [dashboard.dashboard] not defined

Please help how to define this route for this or what is the solution. I am new to Laravel. Thanks.

Upvotes: 1

Views: 7029

Answers (6)

SUDAR MANI
SUDAR MANI

Reputation: 71

Route

Route::get('/dashboard','TestController@dashBoard')->name('dash_board');

Controller

public function dashBoard()
{
   return view('dashboard'); //redirect to view file
}

view

<a href="{{ route('dash_board') }}">DashBoard</a> 

Upvotes: 4

Bruno Francisco
Bruno Francisco

Reputation: 4240

Everyone already answered (especially @ka_lin) your question with if you would like to redirect you just do a simply named route and redirect to it with:

Route::get('/success', function () {
     return view('dashboard/dashboard');
})->name('dashboard.dashboard');

To expand further in the answers it is always a good logic to add every action to a Controller and you may ask why. Because if you would like to optimize your routes by caching by using the command php artisan route:cache you are going to have a problem: this command doesn't work with Closure based routes (a.k.a anonymous functions).

By passing the routes to a Controller you can have big advantages that comes with Class based routes.

An example would be:

Route::get('/dashboard/dashboard','TestController@dashboard' )->name('dashboard.dashboard');

and at the dashboard function you can call the view as you are doing in the Closure:

return view('dashboard/dashboard');

Not to mention that everything stays organized in little logical compartments.

Upvotes: 1

bhavinjr
bhavinjr

Reputation: 1763

try this simple explanation

Route file

Route::get('/dashboard','TestController@dashboard')->name('dashboard');
or
Route::get('/dashboard', ['as'=>'dashboard', 'uses'=>'TestController@dashboard']);

TestController.php

public function dashboard()
{
   return view('dashboard'); //blade file
}

test.blade.php

<html>
<body>
    <h2><a href="{{ route('dashboard') }}">Click Me To Redirect to DashBoard</a> 
    </h2>
</body>
</html>

Upvotes: 1

Future Coder
Future Coder

Reputation: 144

I hope it will work sure

Route::get('/success', function () {
     return view('dashboard/dashboard');
})->name('dashboard.dashboard');

Upvotes: 1

Khaldoun Nd
Khaldoun Nd

Reputation: 1466

To use the route() function you need to give the route a name as follows:

Route::get('/success', function () {
    return view('dashboard/dashboard');
})->name('dashboard.dashboard');

Upvotes: 3

ka_lin
ka_lin

Reputation: 9442

You have to name the route as per documentation:

Route::get('/success', function () {
     return view('dashboard/dashboard');
})->name('dashboard.dashboard');

Upvotes: 3

Related Questions