【问题标题】:Laravel 4 namespaced controller routing with parametersLaravel 4 带参数的命名空间控制器路由
【发布时间】:2014-03-22 01:13:23
【问题描述】:

所以我不知道这是否是因为我的项目是命名空间的,但这就是我的路线:

Route::controller( 'videos/{type?}/{id?}', '\App\Controllers\VideoController@getIndex');
Route::get( 'videos', '\App\Controllers\VideoController' );

/** Home/Fallback Controller **/
Route::controller('/', '\App\Controllers\HomeController');

这是我要去的网址:

mysite.com/videos/supplier/1

这是我的视频控制器:

<?php

namespace App\Controllers;
use \View;
use \Asset;
use \App\Models\Video;

class VideoController extends BaseController {

    public function getIndex($filterType = null, $filterId = null)
    {
        Asset::addStyle('css/magnific-popup.css');
        Asset::addScript('js/magnific-popup.js');
        Asset::addScript('js/magnific-video.js');

        $video = new Video();

        $this->data['videoCategories'] = $video->getCategories();
        $this->data['videoSuppliers'] = $video->getSuppliers();

        if( $filterType == 'category' )
        {
            // grab videos by category id
            $this->data['videos'] = $video->getByCategory( $filterId );
        }
        elseif( $filterType == 'supplier' )
        {
            // grab videos by supplier id
            $this->data['videos'] = $video->getByCategory( $filterId );
        }
        else
        {
            // get all videos
            $this->data['videos'] = $video->getAll();
        }

        $this->data['currentId'] = $filterId;
        $this->data['currentType'] = $filterType;

        $this->layout->content = View::make('videos.index', $this->data);
    }

}

当我访问 mysite.com/videos 时,getIndex 方法工作正常...但是当参数开始起作用时,它不会找到其他路线。我认为它试图找到一个嵌套控制器,如 Controllers/Video/CategoryController@getIndex 或其他东西。 这是它给我的错误:

类\App\Controllers\VideoController@getIndex 不存在

感谢您的帮助!

【问题讨论】:

    标签: routing namespaces laravel-4


    【解决方案1】:

    在你的路由文件中,尝试改变:

    {type?} 到 {any} 和 {id?} 到 {num}

    为我工作...

    或者如果您想要对 {any} 和 {num} 进行更深入的过滤器。 请在路由参数部分(Route::pattern)中的文档中理解

    http://laravel.com/docs/routing#route-parameters

    你可以这样做:

    // remove your @getIndex and change {type?} to {any}, {id?} to {num}
    Route::controller( 'videos/{any}/{num}', '\App\Controllers\VideoController');
    

    【讨论】:

    • 不,当我这样做时它仍然会引发错误。您的项目是否已命名空间?我觉得这就是把它搞砸的原因,虽然我不明白它是怎么回事..
    • 啊,我以为你填错了你的 route::controller 你不应该在 route::controller 中写 @getIndex 见这里:laravel.com/docs/controllers#restful-controllers
    • 哦哇哦..我不小心切换了两个当底部的应该是控制器时,没有得到..谢谢...我觉得很愚蠢=/
    • 你想编辑你的答案以包含它,我可以标记它吗?
    【解决方案2】:

    您正在使用该操作创建路线

    Route::controller( 'videos/{type?}/{id?}', '\App\Controllers\VideoController@getIndex');
    

    你应该:

    Route::controller( 'videos/{type?}/{id?}', '\App\Controllers\VideoController');
    

    【讨论】:

      【解决方案3】:

      对不起,我的英语不好。

      确保您的子文件夹名称以位于 app/controllers 中的情人字母开头

      不正确

      应用程序/控制器/子文件夹

      正确

      应用程序/控制器/子文件夹

      【讨论】:

      • 我认为文件夹的名称无效
      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2013-07-14
      相关资源
      最近更新 更多