Laravel 4命名空间控制器路由参数

时间:2014-03-21 16:37:00

标签: routing namespaces laravel-4

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

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或者其他东西。 这是它给我的错误:

  

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

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

在您的路线文件中,尝试更改:

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

适合我...

或者如果您想要{any}和{num}的更深层过滤器。 请理解路线参数部分(路线::模式)

中的文档

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');

答案 1 :(得分:0)

您正在使用操作

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

你应该只是:

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

答案 2 :(得分:0)

Sory我的不好意思。

确保您的子文件夹名称以app / controllers

中的lovercase字母开头

<强>不正确

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

<强>正确

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

相关问题