您好我已经开始使用Laravel 5并发现它非常有用。现在我在Auth部分。我想要做的是相当于laravel中的这段代码:
if($_SESSION['user'] == $_GET['user'])
{
// I can only see this page
}
这就是我的控制器中的功能:
public function viewProfile(User $user)
{
$cur_user = \Auth::user()->username;
return view('profile', compact('user', 'cur_user'));
}
例如,网址就像这个http://localhost:8000/profile/sorxrob
。 sorxrob
有用户名。我怎样才能Auth::user()
==在laravel中获取网址?
答案 0 :(得分:3)
if(Auth::user()->id == $user->id)
{
$cur_user = \Auth::user()->username;
return view('profile', compact('user', 'cur_user'));
}
答案 1 :(得分:3)
php artisan make:middleware OwnerMiddleware
namespace App\Http\Middleware;
use App\Article;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class OwnerMiddleware
{
/**
* 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)
{
$currentUser = $this->auth->getUser();
if (/* here goes your condition to check user*/) {
abort(403, 'Unauthorized action.');
}
return $next($request);
}
}
app\Http\Kernel.php
:protected $routeMiddleware = [
'owner' => 'App\Http\Middleware\OwnerMiddleware',
];
Route::group(['middleware' => ['owner']], function() {
// your route
});