Laravel请求类反射异常不存在

时间:2018-06-02 23:45:45

标签: php laravel

我在控制器的商店功能中使用名为RolesRequest的请求类收到错误,我的控制器位于控制器下的Admin文件夹中。当我将名称更改为RoleRequest时,我仍然会得到相同的错误而且我不知道为什么,我已经尝试过编写器转储自动加载,但这并没有帮助我仍然得到相同的错误。我使用laravel 5.5.4我认为

错误讯息:

Class App\Http\Requests\RolesRequest does not exist", exception: "ReflectionException"

//控制器

<?php

namespace App\Http\Controllers\Admin;

use App\Role;
use App\Http\Controllers\Controller;
use App\Http\Requests\RolesRequest;
use Illuminate\Http\Request;

class RolesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.roles');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RolesRequest $request)
    {
        dd('hit');
    }
}

// RolesRequest Class

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class RolesRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::user()->hasRole('Admin');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch($this->method())
        {
            case 'GET':
            case 'DELETE':
            {
                return [];
            }
            case 'POST':
            {
                $unique = [
                    'name'      => 'required|min:3|max:30|unique:roles,name',
                ];
                break;
            }
            case 'PUT':
            case 'PATCH':
            {
                $unique = [
                    'name'     => 'required|min:3|max:30|unique:roles,name,'. $this->id
                ];
                break;            
            }
            default:break;
        }      

        $rules = [
            'display_name' => 'required|min:4|max:50',
            'description'  => 'required|min:10|max:100'
        ]  

        return $unique + $rules;
    }
}

1 个答案:

答案 0 :(得分:0)

当发生超过1个错误时,这是​​有趣的时间之一。你看到第二个错误。 第一个错误是解析器在您尝试使用的类中出现语法错误时失败。它试图加载该文件并失败,因此永远不会加载该文件,因此无法找到该类(第二个错误)。

错误页面中显示的错误可能是(2/2)。检查错误日志,看看它是否在您看到的“类不存在”错误之前显示错误。

add文件在public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { base.OnCreate(savedInstanceState); View view = inflater.Inflate(Resource.Layout.my_layout, container, false); Button add = view.FindViewById<Button>(Resource.Id.addbtn); add.Click += delegate { Fragment2 fragment2 = new Fragment2(); FragmentTransaction ft = FragmentManager.BeginTransaction(); ft.Replace(Resource.Id.content_frame, fragment2); ft.Commit(); }; return view; } 之前有语法错误。数组定义语句最后没有分号。