模仿 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');
}

}

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 1

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

3年前 评论

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