gpt4 book ai didi

laravel-5 - Laravel 5 重定向到 Controller Action

转载 作者:行者123 更新时间:2023-12-03 23:42:42 25 4
gpt4 key购买 nike

如果我想重定向到 Controller 的操作。必须在 routes.php 中注册此 Controller 操作?

最佳答案

如果你想使用这样的重定向:

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

此操作必须在 routes.php 文件中注册。

但要小心:它只适用于可靠的 GET 路由。

您可以通过键入来查看可用的操作
php artisan route:list

在您的终端中。

我已经设置了一些用于测试目的的文件(当尝试重定向到非获取方法时,似乎 laravel 正在重定向到具有相同参数签名的可用 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');


});

一个测试 Controller ,用于重定向
<?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);
}
}

另一个 Controller ,我重定向到:
<?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');
}


}

关于laravel-5 - Laravel 5 重定向到 Controller Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36276634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com