What is Laravel Redirections?

Named route is used to give specific name to a route. The name can be assigned using the “as” array key.

File name : index.php

Route::get('user/profile', ['as' => 'profile', function () {
//
}]);

Note − Here, we have given the name profile to a route user/profile.


Anchor Tag Redirection

File name : index.php

<a href="{!! url()->current() !!}">Current path</a>
OR
<a href="{!! url()->full() !!}">Full path</a>
OR
<a href="{!! url('/books/news') !!}">Relative to root path</a>

OUTSIDE EXAMPLE:
OR
<a href="{!! url('http://www.google.com') !!}">Path to google</a>

File name : index.php

<a href="{{url('/login')}}" class="nav-link"><span class="pcoded-micon"><i class="feather icon-server"></i></span><span class="pcoded-mtext">login</span></a>

File name : index.php

<a href="<?=URL::to('/registration')?>" ><span class="pcoded-micon"><i class="feather icon-file-text"></i></span><span class="pcoded-mtext">Registration</span></a>

Redirect to URL

return redirect('user/dashboard');

Redirect back to previous page in Laravel

return redirect()->back();

OR

return redirect()->back()->withInput();

Redirect to Named Routes in Laravel

return redirect()->route('home');

Redirect to Named Routes with parameters in Laravel

return redirect()->route('users', [1]);

Redirect to Controller Action in Laravel

return redirect()->action('App\Http\Controllers\UserController@index');

Redirect to Controller Action With Parameters in Laravel

return redirect()->action('App\Http\Controllers\UserController@index', ['id' => 1]);

Redirect with Flashed Session Data in Laravel

return redirect('home')->with('messgae', 'Welcome to ExpertPHP Tutorials!');

How to Redirect to External Link in Laravel?

return redirect()->away('http://www.itechxpert.in');

Redirect Route With Query String in laravel

redirect with success message or error message in laravel application. so we will use the redirect route with query string in laravel application.

File name : index.php

return redirect('/student')->with('success', 'Data has been added');

Redirect To URL

return redirect('');

Redirect with prarmeter

File name : index.php

return redirect('welcome

return redirect('admin/dashboard');

Redirect To Route

File name : index.php

return redirect()->route('posts.index');

File name : index.php

Route::get('post/{id}', 'PostController@edit')-name('post.edit');

We can return to this route using the below code snippet.
return redirect()->route('post.edit', ['id' => $post->id]);

If you have single parameter

File name : index.php

return redirect()->route('post.edit', $post->id);

Redirect with Data

File name : index.php

return redirect()->back()->with('success', 'Post saved successfully.');

chain multiple with() methods

File name : index.php

return redirect()->back() ->with('success', 'Post saved successfully.') ->with('post_id', $post->id);

File name : index.php

$response = [ 'success' => 'Post saved successfully.', 'post_id' => $post->id, ]; return redirect()->back()->with($response);

Redirect to Controller Action

File name : index.php

return redirect()->action('PostController@store);

File name : index.php

return response()->action('PostController@edit', ['id' => $post->id]);





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial