登录的用户测试有正确的ID在Laravel 4 [英] Test logged in user has correct id in Laravel 4

查看:214
本文介绍了登录的用户测试有正确的ID在Laravel 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何测试登录的用户在Laravel 4可以访问正确的用户帐户。

I'm trying to work out how to test that the logged in user in Laravel 4 can visit the correct user account.

目前,这是在我的routes.php文件的文件。

At the moment this is what is in my routes.php file.

Route::get('user/{id}', array('before' => 'auth', function(){
    // logged in user
    // but can visit any account!!
}));

我将如何限制这使用户/ 1只能看到轮廓如果是登录的用户ID的电流是多少?

How would I restrict this to so user/1 could only see the profile if that is the current logged in users id?

Auth::user()->id

返回登录ID再次进行测试,但我不知道如何写一个过滤器,检查它等于{ID}在URL中。

Returns the logged in id to test against but I can't work out how to write a filter that checks it's equal to the {id} in the url.

请帮帮忙!
谢谢你。

Please help! Thanks.

推荐答案

得到了一些帮助,通过Laravel IRC频道。

Got some help through the Laravel irc channel.

这是我已经用了道路。

Route::filter('user', function($route, $request)
{
    if( $request->segment(2) != Auth::user()->id)
    {
        return Redirect::to('/login');
    }
});

然后我的路线做的。

Then on my route do.

Route::get('user/{id}', array('before' => 'auth|user', 'uses' => 'UsersController@index'));

这篇关于登录的用户测试有正确的ID在Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆