web-dev-qa-db-ja.com

Laravel 5コントローラのアクションにリダイレクトします

コントローラのアクションにリダイレクトしたい場合。このコントローラアクションをroutes.phpに登録する必要がありますか?

14
DengDeng

このようなリダイレクトを使用したい場合:

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

このアクションは、routes.phpファイルに登録する必要があります。

ただし、注意が必要です。信頼できるGETルートでのみ機能します。

次のように入力すると、利用可能なアクションを確認できます

php artisan route:list

あなたのターミナルで。

テスト目的でいくつかのファイルを設定しました(laravelは、get以外のメソッドにリダイレクトしようとすると、同じパラメーターシグネチャで利用可能なGETメソッドにリダイレクトしているようです:

// 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');


});

リダイレクトに使用されるテストコントローラー

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

別のコントローラー、私はリダイレクトしています:

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


}
15
shock_gone_wild

はい routes.phpファイルには、URLとコントローラ/アクションに関する詳細を含める必要があります。

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

これは非常に単純な1行のコードで実行できます。これをチェックするだけです [〜#〜] url [〜#〜]

Laravel。リダイレクトはメソッドをポストすることを意図しています

0
pankaj kumar