有谁知道如何完成这项工作?我正试图在Laravel的路线上创造一个条件。如果是一种类型的用户(例如:Admin),则Dashboard页面将与其他类型的用户(例如:Client)不同。
在此处查看图片 - > http://screenshots.screencast-o-matic.com/screenshot/u/n5AH/1448537774091-31837.png
注意:它们应该位于相同的域domain.com/dashboard中。将呈现给视图的页面必须反映到登录用户的类型。
想法,有人吗?非常感谢你!
答案 0 :(得分:1)
您可能不希望在路线中执行此操作。您应该在控制器上执行此操作。
假设你有一个PageController @ dashboard方法
public function dashboard() {
if($user->admin) {
return view('admin.dashboard');
}
return view ('user.dashboard');
}
答案 1 :(得分:0)
你不能用指向两个动作的相同路线来做,总是会调用最后一个动作,所以最好的方法是通过middleware
,
您需要转到app/Http/Middleware
并创建class isAdministrator
,例如,它应如下所示:
<?php
namespace TPS\Http\Middleware;
use Closure;
use Redirect;
use Illuminate\Contracts\Auth\Guard;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class IsAdministrator
{
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)
{
// I don't know how you verify that your user is an admin or not
// but here we have a role wich can contain many roles
if (! strpos($this->auth->user()->role, 'Administrateur')) {
if ($request->ajax()) {
return response('Forbidden.', 403);
} else {
// now here you will redirect to the normal user dashboard
Redirect::route('user.dashboard');
}
}
return $next($request);
}
}
现在您需要注册中间件并为其命名,转到app/Http/Kernel.php
并将中间件添加到以下阵列:
/**
* The application's route middleware.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \TPS\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \TPS\Http\Middleware\RedirectIfAuthenticated::class,
// I called it admin
'admin' => \TPS\Http\Middleware\IsAdministrator::class,
];
现在您可以将此中间件附加到您的管理仪表板路径(您需要使用命名路由,当您需要更改某些URL时它将会有用):
Route::get('/dashboard', [
'as' => 'admin.dashboard', 'uses' => 'PagesController@userType1'
]);