routing - 带参数的 Laravel 4 命名空间 Controller 路由

标签 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

这是我的视频 Controller :
<?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 方法工作正常......但是当参数起作用时,它不会找到其他路由。我认为它试图找到一个嵌套的 Controller ,比如 Controllers/Video/CategoryController@getIndex 之类的......
这是它给我的错误:

Class \App\Controllers\VideoController@getIndex does not exist



谢谢你的帮助!

最佳答案

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

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

关于routing - 带参数的 Laravel 4 命名空间 Controller 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564424/

相关文章:

ruby-on-rails - Active Admin Ruby on Rails仪表板 Controller 错误

.net - 如何在同一个 IIS 网站下合并 MVC4 站点和 WebAPI 站点?

php - Laravel 5 找不到自定义命名空间的类

java - 在java中将带有命名空间的xml转换为json

php - Laravel 中正确的命名空间

ruby-on-rails - 在 Rails 中更改命名空间

symfony - Symfony 路由中的 ID 超出范围

c++ - 匿名命名空间 - 在头文件和代码文件中都有意义吗?

php - 如何在 Laravel 4 中实现两级关系?

php - MySQL 错误 1054