【问题标题】:Checking if route parameter is being called检查是否正在调用路由参数
【发布时间】:2013-10-27 00:51:16
【问题描述】:

在路由过滤器中,我试图确定是否调用了路由参数,它可能为 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
    }   
}

【问题讨论】:

  • “路由参数”是什么意思?是那个 URL 参数吗?
  • 路由的参数,如应用于该路由的变量中... AKA Route::post('{agency}/job/view/{job}', 'Controllers\ Agency\JobController@postEdit');

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 2014-07-21
    • 1970-01-01
    • 2017-07-06
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    相关资源
    最近更新 更多