【问题标题】:laravel route findOrFail() problemslaravel 路由 findOrFail() 问题
【发布时间】:2017-04-06 18:49:08
【问题描述】:

Laravel 的新功能,我正在尝试在这条特定路线上添加一个 findOrFail ,这让我很难过。我错过了什么?

Route::get('/listing/{type}/{owner}/{id}/{address}', 'Properties\DisplayController@show');

什么不工作

Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
    return Properties\DisplayController@show::findOrFail($id);
});

我得到的错误

解析错误:语法错误,意外'@',期待';'

我正在调用的控制器/函数

 public function show($type, $own, $id, $address = null)
    {
        $page                = (object) $this->template;
        $page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));

        // Now lets query our server
        $client   = new GuzzleHttp\Client(['verify' => false ]);
        $response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id);

        $page->content = Property::parseResult($response->getBody());

        $page->title = strtoupper(trim($page->content->address));

        $page->breadcrumbs[] = array('text' => $page->title);

        $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
        $currency = 'CAD';
        $raw = $formatter->parseCurrency($page->content->price, $currency );

        $page->content->mortgage = Mortgage::stage(
            false,
            $raw
        );

        return view('property.display', compact('page'));
    }

感谢您的帮助!

【问题讨论】:

  • findOrFail() is a model method, do you want to do this on a controller?
  • 路由本身不能调用吗?
  • 不,你不能。事实上你也不能这样做return Properties\DisplayController@show。您在 php 中使用 ->(或 :: 用于静态方法)调用对象方法,而不是 @。第一个示例(这是唯一的路由方式,除了使用匿名函数)之所以有效,是因为您将它作为字符串传递,Laravel 会解析字符串以了解要创建哪个对象(控制器)以及调用哪个方法。
  • 谢谢,所以我真的必须进入模型。谢谢!

标签: php laravel laravel-5 eloquent


【解决方案1】:

直接返回路线:

Route::get('/listing/{type}/{owner}/{id}/{address}', function ($id) {
    return App\YourModel::findOrFail($id);
});

https://laravel.com/docs/5.3/eloquent#retrieving-single-models

【讨论】:

    【解决方案2】:

    由于模型位于我们使用 GuzzleHTTP 连接的另一台服务器上,我无法将 findOfFail() 放在模型上。

    这是对控制器的编辑。在['http_errors' => false] 中添加了防止guzzle 返回http 错误,然后使用getStatusCode() 的if 语句来查找它是否是错误500。

    public function show($type, $own, $id, $address = null)
        {
            $page                = (object) $this->template;
            $page->breadcrumbs[] = array('url' => 'javascript://', 'text' => 'Property Search', 'attribute' => array('data-component' => 'back'));
    
            // Now lets query our server
            $client   = new GuzzleHttp\Client(['verify' => false ]);
            $response = $client->get( env('LISTINGS_SERVER', 'https://listings.homicity.com') . '/property/' . $id, ['http_errors' => false]);
    
            if ($response->getStatusCode() == "500") {
                abort(404);
            }
            else {
                $page->content = Property::parseResult($response->getBody());
    
                $page->title = strtoupper(trim($page->content->address));
    
                $page->breadcrumbs[] = array('text' => $page->title);
    
                $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
                $currency = 'CAD';
                $raw = $formatter->parseCurrency($page->content->price, $currency );
    
                $page->content->mortgage = Mortgage::stage(
                    false,
                    $raw
                );
    
                return view('property.display', compact('page'));
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-14
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多