Laravel 5 redirects to controller actions

If I want to redirect to controller action. Should this controller action be logged in routes.php ?

+11
source share
2 answers

If you want to use redirection as follows:

 return redirect()->action(' AnotherController@someMethod '); 

This action must be logged in the routes.php file.

But be careful: it only works with reliable GET routes.

You can see the available actions by typing

 php artisan route:list 

in your terminal.

I installed some files for testing purposes (it seems that laravel redirects to available GET methods with the same parameter signature when trying to redirect to non-get methods:

 // routes.php Route::group(['middleware' => ['web']], function () { Route::get('start', ' TestController@start '); // routes, we could redirect to // get route Route::get('test', ' AnotherController@test '); // post route Route::post('testPost', ' AnotherController@testPost '); // setup a resource with index, store, update, delete and show actions Route::resource('resource', 'AnotherController'); }); 

Test controller that is used to redirect

 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class TestController extends Controller { public function start() { // works return redirect()->action(' AnotherController@test '); // method not allowed exception return redirect()->action(' AnotherController@testPost '); /** * Redirecting to routes setup via Route::resource */ // works return redirect()->action(' AnotherController@index '); // redirects to ' AnotherController@index ' return redirect()->action(' AnotherController@store '); // error: Missing required parameters for [Route: resource.destroy] [URI: resource/{resource}]. return redirect()->action(' AnotherController@destroy '); // redirects to ' AnotherController@show ' return redirect()->action(' AnotherController@destroy ', 1); // Missing required parameters for [Route: resource.update] [URI: resource/{resource}]. return redirect()->action(' AnotherController@update '); // redirects to ' AnotherController@show ' return redirect()->action(' AnotherController@update ', 1); } } 

Another controller, I redirect to:

 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class AnotherController extends Controller { public function test() { dd('i am test'); } public function testPost() { dd('i am testPost'); } /** * Resourceful routes below */ public function index() { dd ('I am index'); } public function store() { dd ('I am store'); } public function destroy($id) { dd('I am destroy'); } public function show($id) { dd('I am show'); } public function update($id) { dd('I am update'); } } 
+13
source

Yes, the routes.php file should contain information about the URL and controller / action:

 // Get route Route::get('/page', ' MyController@action '); // Post route Route::post('/login', ' LoginController@login '); 
0
source

Source: https://habr.com/ru/post/1245960/


All Articles