类Illuminate \ Routing \ Middleware \ Rolemiddleware不存在

时间:2016-10-23 06:33:02

标签: php laravel

我正在使用laravel,当我试图在路由器中注入中间件时出现一些错误

我的路由器代码

$query = "SELECT * FROM `users` WHERE status='accepted'";
$result = mysql_query($query) or die(mysql_error()); 

echo '<table class="table">';
echo '<h2>Accounts</h2>';
echo '<tr class="table">';
echo '<th class="table">Name</th>';
echo '<th class="table">Email</th>';
echo '<th class="table">Username</th>';
echo '<th class="table">Date Created</th>';
echo '<th class="table">Decline</th>';
echo '<th class="table">Accept</th>';
echo '<th class="table">Give Admin</th>';
echo '</tr>';

while($row = mysql_fetch_assoc($result))
{
    echo '<tr class="table">';
    echo '<th class="table">' . $row['name'] . '</th>';
    echo '<th class="table">' . $row['email'] . '</th>';
    echo '<th class="table">' . $row['username'] . '</th>';
    echo '<th class="table">' . $row['trn_date'] . '</th>'; 
    echo '<form method="post">';
    echo '<th class="table"><input type="submit" name="decline_' .$row["id"]. '" value="decline" ></input></th>';
    echo '<th class="table"><input type="submit" name="accept_' .$row["id"]. '" value="accept" ></input></th>';
    echo '<th class="table"><input type="submit" name="giveadmin_' .$row["id"]. '" value="accept/give admin" ></input></th>';
    echo '</form>';
    echo '</tr>';
}

echo '</table>';

kernal.php

<?php

Route::get('role',[
    'middleware' => 'Role:editor',
    'uses' => 'TestController@index',
])->middleware('role');

?>

RoleMiddleware.php

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'Age' => \App\Http\Middlware\AgeMiddleware::class,
        'Role' => \App\Http\Middlware\RoleMiddleware::class,
    ];

&GT;

这是我的TestController代码:

<?php

namespace App\Http\Middleware;

use Closure;

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        echo "Role: ".$role;
        return $next($request);
    }
}

我收到错误:类Illuminate \ Routing \ Middleware \ Rolemiddleware不存在。我是Laravel的新手并使用Laravel 5.3。你能告诉我为什么它会给我一个错误

1 个答案:

答案 0 :(得分:0)

在kernal.php中,您在单词Middlware

中拼写错误

更改此

'Age' => \App\Http\Middlware\AgeMiddleware::class,
  'Role' => \App\Http\Middlware\RoleMiddleware::class,

'Age' => \App\Http\Middleware\AgeMiddleware::class,
'Role' => \App\Http\Middleware\RoleMiddleware::class,