gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:00:27 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com