duanleiliu7373 2014-05-28 04:13
浏览 44
已采纳

未找到Laravel控制器方法

Trying to run the following Laravel 4.1 route: http://myserver.dev/admin/import-items/1

When I do so, I get the following error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.

Here are my routes for this:

Route::group(array('prefix' => 'admin', 'before' => 'auth'), function()
{
    Route::get('items/import-items/{after?}', array('as' => 'importItems', 'uses' => 'ItemsController@importItems'));

    Route::get('items/{id}/show', 'ItemsController@show');
    Route::resource('items', 'ItemsController');
});

I can look at the Items controller, and the method importItems is most definitely there:

class ItemsController extends \BaseController {

    /**
     * Item Model
     * @var Item
     */
    protected $item;

    /**
     * Inject the models.
     * @param Item $item
     */
    public function __construct(Item $item)
    {
        parent::__construct();

        $this->item = $item;
    }

    /**
     * Display a listing of items
     *
     * @return Response
     */
    public function index($items = [])
    {
        $title = Lang::get('admin/items/title.manage_items');

        if (empty($items))
            $items = $this->item;

        return View::make('admin/items/index', compact('items', 'title'));
    }

    /**
     * Imports Items after specified date.
     * @return array
     **/
    public function importItems($after = 7)
    {

        $results = Item::importItems($after);

        return $results;
    }
}

When I run php artisan routes the route clearly shows up as usable in the list:

GET|HEAD admin/items/import-items/{after?} | importItems   | ItemsController@importItems

The thing is, this exact code works just fine from another project I had it in. After I copied over the route settings and the controller and model, it decided not to work in this new project. I feel like I've missed some key step here because I can't see any differences in the code.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douan6931 2014-05-28 05:18
    关注

    You are using the wrong url. This url:

    http://myserver.dev/admin/import-items/1
    

    should be this instead

    http://myserver.dev/admin/items/import-items/1
    

    OR

    Route::get('items/import-items/{after?}', array('as' => 'importItems', 'uses' => 'ItemsController@importItems'));
    

    should be

    Route::get('import-items/{after?}', array('as' => 'importItems', 'uses' => 'ItemsController@importItems'));
    

    ...depends which url you want

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算