我升级到Laravel 5.0,70%的应用程序正在运行,但我的登录有问题。我有宏 - > html :: macro和response :: macro。 html :: macro正在运行 - 我使用html :: macros进行导航。但是如果我需要一个响应::宏,我会收到这个错误:
exception 'BadMethodCallException' with message 'Method ajaxRedirect does not exist.'
这是我的电话:
public function postAuthenticate()
{
try
{
$this->service->authenticate(Input::except(['_token', 'extended']));
}
catch(AuthenticationNotValidException $ex)
{
throw new AuthenticationNotValidAjaxException($ex->getMessage());
}
if (Session::has('tempRequest'))
{
$this->service->addTemporaryRequest(Auth::user());
}
return Response::ajaxRedirect(route(
(Auth::user()->isAdmin()) ? 'admin.dashboard' : 'customer.dashboard'));
}
这里的宏:
Response::macro('ajaxRedirect', function($url)
{
return Response::json(array(
'state' => true,
'redirect' => $url
));
});
为什么我收到此错误,我会错过什么吗?在Laravel 4,一切都运转良好。任何提示都表示赞赏。
答案 0 :(得分:1)
参考文献:
以下是制作自定义/宏laravel-5响应
的方法<?php namespace App\Providers;
use Response;
use Illuminate\Support\ServiceProvider;
class ResponseMacroServiceProvider extends ServiceProvider {
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Response::macro('ajaxRedirect', function($url)
{
return Response::json(array(
'state' => true,
'redirect' => $url
));
});
}
}
并将其保存为ResponseMacroServiceProvider.php
中的app/Providers
。
<?php
return [
...
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
...
'App\Providers\ResponseMacroServiceProvider',
],
...
config/app.php
中的。
您应该可以在控制器中使用它
return Response::ajaxRedirect(route(
(Auth::user()->isAdmin()) ? 'admin.dashboard' : 'customer.dashboard'));
根据需要。