在中间件方法中找不到类名 - Laravel 5


class name not found in middleware method - Laravel 5

我正在尝试设置中间件。我按照以下说明操作:

http://mattstauffer.co/blog/laravel-5.0-middleware-filter-style

我的代码是

<?php namespace App'Http'Middleware;
use Closure;
use Illuminate'Http'RedirectResponse;
class LoadVars {
$comingevents = App'Number::where('item','events')->get(array('quantity'));

我收到此错误:

LoadVars 中的 FatalErrorException .php第 24 行:找不到类"应用程序''http''中间件''应用程序''编号"

在模型中,当我定义关系时,我使用App''Number,它运行良好。

在中间件方法中使用类的正确方法是什么?

正如@Quasdunk在注释中指出的那样,当您引用开头没有反斜杠的类时,路径是相对的
这意味着App'Number将在当前命名空间中查找App,然后Number .

App'Http'Middleware  &  App'Number   =>  App'Http'Middleware'App'Number

您只需要在开始时添加一个',路径将被解释为绝对路径,实际上从您使用类的位置无关紧要

App'Http'Middleware  &  'App'Number  =>  App'Number
Foo'Bar              &  'App'Number  =>  App'Number

如果你喜欢你的代码更简洁一点,你也可以用use语句导入类:

use App'Number;
class LoadVars {
    // ...
    $comingevents = Number::where('item','events')->get(array('quantity'));
    // ...
}

请注意,对于 use 语句,不需要反斜杠。所有路径都是绝对的。