首页 > 解决方案 > 如何解决找不到类“App\Http\Requests\Web\WebRequest”

问题描述

我在 App\Http\Requests\Web 中创建了一个请求,其中显示了错误。

找不到类“App\Http\Requests\Web\WebRequest”

这是我的请求 CreateBucket.php 的代码:

<?php

namespace App\Http\Requests\Web;

    class CreateBucket extends WebRequest
    {
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'bucket_name' => 'required|string|string|max:30',
            'bucket_type' => 'required|string|string|max:30',
            'bucket_image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg',
        ];
    }
}

这是我的桶控制器代码:

<?php

namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Web\CreateBucket;
use App\Bucket;

class BucketController extends Controller
{
public function index(Request $request)
{
    $buckets = Bucket::orderBy('id','ASC')->paginate(10);
    return view('buckets.index',compact('buckets',$buckets))
    ->with('i',($request->input('page',1) - 1) * 10); 
}

public function create()
{
    return view('buckets.create');
}

public function store(CreateBucket $request)
{
    if($request->hasFile('bucket_image')) {
        $bucket_image = $request->file('bucket_image');
        $bucket_image_name = time().'.'.$bucket_image->getClientOriginalExtension();
        $path = public_path('Storage/BucketImages');
        $bucket_image->move($path, $bucket_image_name);
        $bucket_image = 'Storage/BucketImages/'.$bucket_image_name;
    } else {
        $bucket_image = NULL;
    }

    $category = Category::create([
        'bucket_name' => $request->input('bucket_name'),
        'bucket_image'=> $bucket_image,
        'bucket_type' => $request->input('bucket_type'),
    ]);

    return redirect()->route('buckets.index')
                    ->with('success','Bucket created successfully');
}

请帮助我解决此错误。谢谢。

标签: laravel-5

解决方案


我的 WebRequest.php 在 Requests 文件夹中丢失,这就是他给我这个错误的原因。这是我创建的 WebRequest.php 文件,我的问题已解决。

<?php

namespace App\Http\Requests\Web;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class WebRequest extends FormRequest
{
/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        //
    ];
}
} 

推荐阅读