Laravel过滤器无法正常工作

时间:2014-08-05 13:38:11

标签: php laravel-4

首先,我确信这些过滤器最初是在工作的,我没有注意到它们不是。

我来自filters.php的过滤器

Route::filter('partnership', function()
{
    if (!Auth::check())
    {
        return Redirect::to('login');
    }
    elseif(Auth::check())
    {
        $access = Auth::user()->access;
        if(!$access == 'partnership')
        {
            return Redirect::to('login');
        }
    }
});

Route::filter('company', function()
{
    if (!Auth::check())
    {
        return Redirect::to('login');
    }
    elseif(Auth::check())
    {
        $access = Auth::user()->access;
        if(!$access == 'company')
        {
            return Redirect::to('login');
        }
    }
});

这些用于查看用户是否具有正确的访问级别

我的路线组在routes.php

中看起来像这样

编辑:错过了第一个过滤器组的这一部分

Route::group(array('before' => 'partnership'), function ()
{
//Dashboard access
    Route::get('partnership_dashboard', array('as' => 'partnership_dashboard', function()
    {
        $count = DB::table('devices')->count();
        if ($count > 25)
        {
            $overLicense = $count - 25;
            $licenseWarning = 'The client is over their license quota by '.$overLicense;
            return View::make('partnership.partnership-dashboard')
                ->withErrors($licenseWarning);
        }
        else
        {
            return View::make('partnership.partnership-dashboard');
        }
    }));

    // Route for register form action
    Route::post('register', array('uses' => 'UsersController@partnership_create'));

我的公司小组

Route::group(array('before' => 'company'), function ()
{
    //Dashboard access
    Route::get('dashboard', array('as' => 'dashboard', function()
    {
        // Get user data for dashboard drop down menu
        $users = DB::table('users')
            ->where('access', 'company')->get();
        $i = 0;
        foreach ($users as $user) {
            $full_name = $user->first_name." ".$user->last_name;
            $user_arr[$i] = array('name' => $full_name, 'user_id' => $user->id);
            $i++;
        }
        // Get device data for dashboard dropdown menu
        $devices = DB::table('devices')->get();
        $count = DB::table('devices')->count();
        if ($count > 25)
        {
            $overLicense = $count - 25;
            $licenseWarning = 'You are over your license quota by '.$overLicense; 
            return View::make('dashboard')
                ->with('users', $user_arr)
                ->with('devices', $devices)
                ->withErrors($licenseWarning);
        }
        else {
        return View::make('company.dashboard')
            ->with('users', $user_arr)
            ->with('devices', $devices);
        }
    }));
});

我已经检查过我从数据库中获取了正确的数据。我只是有点难过

提前致谢

1 个答案:

答案 0 :(得分:1)

if(!$access == 'partnership')

我认为你在这一行中的意思(在公司过滤器中类似)是这样的:

if($access != 'partnership')

从第一种方式来看,你可以拥有一个等于伙伴关系的访问级别,这个语句将返回true:

$access == 'partnership'

抛出前面的!否定了真值,所以它结果是假的,你最终会出现逻辑错误。