重定向到 Laravel 5 中不起作用的路由

Redirect to route not working in Laravel 5

我有一个应用程序,用户可以在其中提交一个表单,该表单执行 SOAP 交换以从 Web API 获取一些数据。如果某个时间内请求过多,Throttle服务器拒绝访问。我为这个名为 throttle.blade.php 的自定义错误视图保存在 resources\views\pages 下。在 routes.php 中,我将路线命名为:

Route::get('throttle', 'PagesController@throttleError');

PagesController.php中我添加了相关函数为:

public function throttleError() {
    return view('pages.throttle');
}

这是我为执行 SOAP 交换而创建的 SoapWrapper class:

<?php namespace App\Models;

use SoapClient;
use Illuminate\Http\RedirectResponse;
use Redirect;

class SoapWrapper {

public function soapExchange() {

    try {
        // set WSDL for authentication
        $auth_url = "http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl";

        // set WSDL for search
        $search_url = "http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl";

        // create SOAP Client for authentication
        $auth_client = @new SoapClient($auth_url);

        // create SOAP Client for search
        $search_client = @new SoapClient($search_url);

        // run 'authenticate' method and store as variable
        $auth_response = $auth_client->authenticate();

        // add SID (SessionID) returned from authenticate() to cookie of search client
        $search_client->__setCookie('SID', $auth_response->return);

    } catch (\SoapFault $e) {
        // if it fails due to throttle error, route to relevant view
        return Redirect::route('throttle');
    }
}
}

一切正常,直到我达到节流服务器允许的最大请求数,此时它应该显示我的自定义视图,但它显示错误:

InvalidArgumentException in UrlGenerator.php line 273:
Route [throttle] not defined.

我不明白为什么说没有定义路由。

您没有为路线定义名称,只是一条路径。您可以这样定义路线:

Route::get('throttle', ['as' => 'throttle', 'uses' => 'PagesController@throttleError']);

该方法的第一部分是您定义的路由路径,如 /throttle。作为第二个参数,您可以传递带有选项的数组,您可以在其中指定路由的唯一名称 (as) 和回调(在本例中为控制器)。

您可以在 documentation 中阅读更多关于路线的信息。