升级到laravel 5.0

时间:2015-05-05 13:35:15

标签: php laravel macros laravel-5

我升级到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,一切都运转良好。任何提示都表示赞赏。

1 个答案:

答案 0 :(得分:1)

参考文献:

以下是制作自定义/宏响应

的方法

创建服务提供商

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

根据需要。