如何将请求重定向到来自 jeffochoa 教程的 laravel 中的特定路由
How to redirect a request to a specific-route in laravel from jeffochoa tutorial
我正在尝试通过 jeff ochoa 实现此示例代码,它允许您将请求重定向到 laravel 中的特定路由。
https://jeffochoa.me/redirect-a-request-to-a-specific-route-laravel
我在 运行 代码时遇到此错误:
Got error 'PHP message: [31 /.../app/Providers/RouteServiceProvider.php 0 Argument 1 passed to Illuminate\Routing\Router::App\Providers\{closure}() must be an instance of Request, instance of Illuminate\Http\Request given]'
谁能告诉我哪里出错了? $request 对象在收到时已经预定义为 Illuminate\Http\Request ,所以是否可以转换它或 ??
这是我当前的代码...
我已将此路线添加到 /routes/api。php
Route::group(['namespace' => 'API'],function() {
Route::get('/test',"testAPIController@get");
Route::get('macro.test', ['as' => 'macro.test', 'uses' => 'testAPIController0@get']);
});
我已将 Route::macro 添加到 /app/providers/RouteServiceProvider。php:
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::macro(
'sendToRoute',
function (Request $request, string $routeName) {
$route = tap($this->routes->getByName($routeName))->bind($request);
$this->current = $route;
return $this->runRoute($request, $this->current);
}
);
parent::boot();
}
调用宏的控制器:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Route;
class testAPIController extends AppBaseController
{
public function get(Request $request)
{
return Route::sendToRoute($request,"macro.test");
}
}
?>
目标控制器:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
class testAPIController0 extends AppBaseController
{
public function get(Request $request) {
die("Hello World");
}
}
?>
在定义宏函数时,应该注册Illuminate\Http\Request
而不是只注册Request
。像这样:
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::macro(
'sendToRoute',
function (\Illuminate\Http\Request $request, string $routeName) {
$route = tap($this->routes->getByName($routeName))->bind($request);
$this->current = $route;
return $this->runRoute($request, $this->current);
}
);
parent::boot();
}
当然你也可以在php文件之上导入Illuminate\Http\Request
我正在尝试通过 jeff ochoa 实现此示例代码,它允许您将请求重定向到 laravel 中的特定路由。
https://jeffochoa.me/redirect-a-request-to-a-specific-route-laravel
我在 运行 代码时遇到此错误:
Got error 'PHP message: [31 /.../app/Providers/RouteServiceProvider.php 0 Argument 1 passed to Illuminate\Routing\Router::App\Providers\{closure}() must be an instance of Request, instance of Illuminate\Http\Request given]'
谁能告诉我哪里出错了? $request 对象在收到时已经预定义为 Illuminate\Http\Request ,所以是否可以转换它或 ??
这是我当前的代码...
我已将此路线添加到 /routes/api。php
Route::group(['namespace' => 'API'],function() {
Route::get('/test',"testAPIController@get");
Route::get('macro.test', ['as' => 'macro.test', 'uses' => 'testAPIController0@get']);
});
我已将 Route::macro 添加到 /app/providers/RouteServiceProvider。php:
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::macro(
'sendToRoute',
function (Request $request, string $routeName) {
$route = tap($this->routes->getByName($routeName))->bind($request);
$this->current = $route;
return $this->runRoute($request, $this->current);
}
);
parent::boot();
}
调用宏的控制器:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Route;
class testAPIController extends AppBaseController
{
public function get(Request $request)
{
return Route::sendToRoute($request,"macro.test");
}
}
?>
目标控制器:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
class testAPIController0 extends AppBaseController
{
public function get(Request $request) {
die("Hello World");
}
}
?>
在定义宏函数时,应该注册Illuminate\Http\Request
而不是只注册Request
。像这样:
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::macro(
'sendToRoute',
function (\Illuminate\Http\Request $request, string $routeName) {
$route = tap($this->routes->getByName($routeName))->bind($request);
$this->current = $route;
return $this->runRoute($request, $this->current);
}
);
parent::boot();
}
当然你也可以在php文件之上导入Illuminate\Http\Request