Я пытаюсь расширить Laravel-5.1 Auth
промежуточное программное обеспечение, так что я могу добавить свой собственный метод к нему:Laravel-5 добавив метод hasRole авторизовать
Auth::hasRole()
Что мне нужно сделать в добавить новый метод hasRole to Auth?
Вот файл мои маршруты:
/* Administrator Routes */
Route::group(['namespace' => 'Admin', 'middleware' => 'timesheets.admin:Administrator'], function()
{
Route::get('home', '[email protected]');
});
Вот мой промежуточного файла:
<?php
namespace App\Http\Middleware\Auth;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class AdminAuthenticate
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
if (auth()->check() && auth()->user()->hasRole($role)) {
return $next($request);
}
}
}
Если вы хотите, чтобы это часть класса Auth, а не модель User, вам необходимо расширить класс Auth. – ceejayoz