检查是否正在调用路由参数

时间:2013-10-18 08:42:40

标签: php routing laravel laravel-4 ioc-container

在尝试确定路由参数是否已被调用的路由过滤器中,它可能是NULL但我仍然需要知道它是否被调用...

e.g。

if( // IS ROUTE "job" being called ? ) {

    if( is_null($job = $route->getParameter('job')) ) {

        return App::abort(404, 'This job does not exist.'); // Show the not found page
    }
    elseif( $job->agency->id != $agency->id ) {

        return App::abort(403, 'You are not authorized to view this job.'); // Show the insufficient permissions page
    }   
}

1 个答案:

答案 0 :(得分:0)

所以我解决了自己的问题,我不确定它是传统的还是优雅的方式:

in_array('job', $route->getParameterKeys())

检查是否已在当前路由上调用路由参数“job”。非常有用。

我之前的代码现在看起来像:

if( in_array('job', $route->getParameterKeys()) ) {

    if( is_null($job = $route->getParameter('job')) ) {

        return App::abort(404, 'This job does not exist.'); // Show the not found page
    }
    elseif( $job->agency->id != $agency->id ) {

        return App::abort(403, 'You are not authorized to view this job.'); // Show the insufficient permissions page
    }   
}