Lloople
Lloople

Reputation: 1824

Laravel 5 custom module routes not working

I'm creating my custom modules for my projects to be able to add some features to a project or another depending of the requeriments.

My issue is with the routes, I load the routes in a ModuleServiceProvider loaded in app.php:

include __DIR__.'/../../modules/canae/Http/routes.php';

I checked that this works with an echo inside that file. The routes.php file contains the following code:

Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
    Route::controller('dogs', 'Canae\Http\Controllers\Admin\DogController');
});

I also checked that Laravel can find the Controller, the problem is that it's unable to execute the code inside it.

Here's the code I have inside DogController:

<?php namespace Canae\Http\Controllers\Admin;

class DogController extends \Origin\Http\Controllers\Controller {
    public function getIndex() {
        echo "Hello!";die();
    }
}

And the error is Controller method not found.

If I modify the extends below to Origin\Http\Controllers\Controller (removing the first \) I get the following error: Class 'Canae\Http\Controllers\Admin\Origin\Http\Controllers\Controller' not found so my conclusion is that the code inside this controller is executing, at least reading from Laravel.

Also I'm trying to achieve the Index function with this route http://localhost/canae/public/admin/dogs/index.

This is the tail result of executing php artisan route:list:

|        | GET|HEAD                       | admin/dogs/index/{one?}/{two?}/{three?}/{four?}/{five?}                       |        | Canae\Http\Controllers\Admin\DogController@getIndex                | auth       |
|        | GET|HEAD                       | admin/dogs                                                                    |        | Canae\Http\Controllers\Admin\DogController@getIndex                | auth       |
|        | GET|HEAD|POST|PUT|PATCH|DELETE | admin/dogs/{_missing}                                                         |        | Canae\Http\Controllers\Admin\DogController@missingMethod           | auth       |
+--------+--------------------------------+-------------------------------------------------------------------------------+--------+--------------------------------------------------------------------+------------+

Tell me if you need more information. And thanks for your time.

Upvotes: 1

Views: 4076

Answers (2)

Lloople
Lloople

Reputation: 1824

I solved it moving the line inside providers that loads this routes to the first item of the providers array, even before the application ones. Don't know why, but now it's working.

Upvotes: 2

C Hunter
C Hunter

Reputation: 76

In light of the docs at: http://laravel.com/docs/master/controllers

Have you tried using the "use" statement? Your code would then look like:

<?php 

namespace Canae\Http\Controllers\Admin;

use Canae\Http\Controllers\Controller;

class DogController extends Controller {
    public function getIndex() {
        echo "Hello!";die();
    }
}

I'm also not sure why your namespace is "Canae\Http\Controllers\Admin" as the example shows "App\Http\Controllers" only. I'm not familiar with the specific structure of your project, but removing the "\Admin" might also help.

Upvotes: 0

Related Questions