0

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

例如

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
    }   
}
4

1 回答 1

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
    }   
}
于 2013-10-18T08:55:24.413 回答