模仿 TP5 写了 error 和 success 方法

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Exceptions\HttpResponseException;
use Auth;

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

protected function dispatch_success_tmpl() {
    return 'jump.dispatch_jump';
}

protected function dispatch_error_tmpl() {
    return 'jump.dispatch_jump';
}

// 操作成功跳转的快捷方法
protected function success($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
    session()->flash('success', $msg);

    if (is_null($url)) {
        $url = url()->previous();
    }

    $result = [
        'code' => 1,
        'msg'  => $msg,
        'data' => $data,
        'url'  => $url,
        'wait' => $wait,
    ];

    if(request()->ajax()) {
        $response =  response()->json($result)->withHeaders($header);
    } else {
        $response =  response()->view($this->dispatch_success_tmpl(), $result)->withHeaders($header);
    }

    throw new HttpResponseException($response);
}

// 操作失败跳转的快捷方法
protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) {
    session()->flash('warning', $msg);

    if (is_null($url)) {
        $url = request()->ajax() ? '' : 'javascript:history.back(-1);';
    }

    $result = [
        'code' => 0,
        'msg'  => $msg,
        'data' => $data,
        'url'  => $url,
        'wait' => $wait,
    ];

    if(request()->ajax()) {
        $response = response()->json($result)->withHeaders($header);
    } else {
        $response = response()->view($this->dispatch_error_tmpl(), $result)->withHeaders($header);
    }

    throw new HttpResponseException($response);
}

public function _logout() {
    Auth::logout();
    session()->flash('success', '您已成功退出');
    return redirect('login');
}

}

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

请问jump.dispatch_jump文件怎么写呢?还有这个方法具体怎么调用呢

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!