简体   繁体   中英

Laravel controller method not found

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM