Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 redirect to controller actions

If I want to redirect to a controller's action. Must this controller action be registered in routes.php ?

like image 265
DengDeng Avatar asked Mar 29 '16 06:03

DengDeng


People also ask

How would you redirect back to a controller action?

Redirect to a Controller Action The last piece of the puzzle is that you can redirect to a specific Method of a specific Controller, no matter what its Route or URL is – use method action(): return redirect()->action('App\Http\Controllers\BooksController@index');


1 Answers

If you want to use a redirection like this:

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

This action has to be registered in the routes.php file.

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

You can see you available actions by typing

php artisan route:list

in your terminal.

I have set up some files for testing purposes (seems like laravel is redirecting to available GET methods with 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');


});

A Test Controller, that is used for redirecting

<?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 am redirecting 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');
    }


}
like image 123
shock_gone_wild Avatar answered Sep 28 '22 10:09

shock_gone_wild