There controller:
class HomeController extends BaseController {
public function index() {
return View::make('hello');
}
}
and in the presence of the router:
Route::get('/', 'HomeController#index');
an error:
BadMethodCallException
Method [index] does not exist.
The command php artisan routes, returns what you need:
+--------+------------+------+----------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------+------+----------------------+----------------+---------------+
| | GET|HEAD / | | HomeController#index | | |
+--------+------------+------+----------------------+----------------+---------------+
Version: Laravel 4.2.11
Russian version of the question
There are two reasons this would be happening.
There is another controller somewhere called HomeController.
Your controller is namespaced.
If it's the first, remove the other version and if it's the second, add the namespace to the 'uses' parameter in the router definition.
Related
I have two controllers and I try pass id of variable form method one controller to method to second controller and I got an error like this >MethodNotAllowedHttpException . I will add that my url after the action looks like this >http://localhost/comment?12 . How is the best way of solving this problem ?
You are most probably getting MethodNotAllowedException, because you are opening a route that is defined as a POST route via GET or the other way around.
To avoid that you can use php artisan route:list and get a list of all defined routes and see how you should "access" them:
+--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
| | GET|HEAD | /a/show/{id} | | App\Http\Controllers\AController#show | web |
| | GET|HEAD | /b/show/{id} | | App\Http\Controllers\BController#show | web |
So let's say you have 2 controllers: AController and BController. Each of the controllers have a show() method declared in them.
class AController extends Controller {
//... other AController related code
public function show($id) {
dd($id);
}
}
class BController extends Controller {
//... other BController related code
public function show($id) {
dd($id);
}
}
Then you can define your routes like this:
Route::get('/a/show/{id}', 'AController#show');
Links like: example.com/a/show/10 will "load" AController's show() method. All we have in our AController::show() method's body is dump and die on $id, we will get 10 printed if we visit that link.
We can replace that dd($id); with:
redirect()->action('BController#show', ['id' => $id]);
And define another route:
Route::get('/b/show/{id}', 'BController#show');
This way if we open the previous link: example.com/a/show/10, we will be redirected to: example.com/b/show/10 and BController::show() method will be executed and it prints the variable using dump and die.
Key points:
Route Parameters
Controller Parameters
redirect()-ing
Any help why this is not working,I am using Laravel 5.4 version ,this is are my routes
app\Providers\RouteServiceProvider.php
public function map()
{
$this->mapWebRoutes();
$this->mapExampleRoutes();
}
protected function mapExampleRoutes()
{
Route::prefix('example')
->middleware('example')
->namespace($this->namespace.'\\Examle')
->group(base_path('routes/example.php'));
}
routes\example.php
Route::get('/{any}', function () {
return view('example.app');
})->where('any', '.*');
$ php artisan route:list
+--------+-----------+-----------------+------+----------+-------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------+------+----------+-------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | example/{any} | | Closure | example |
+--------+-----------+-----------------+------+----------+-------------+
The problem is when I try to access /example it returns not found (NotFoundHttpException) ,
other routes are working , for example, /example/login .
any idea why this one is not working ?
Route::get('{any?}', function () {
return view('example.app');
})->where('any', '.*');
I removed the leading slash (/) and added a question mark (?) to indicate the slug is optional.
I'm getting a 404 error when trying to access a route linked to a controller action.
I have the route defined like this in my routes.php file.
Route::controller('error', 'ErrorsController');
The ErrorsController class looks as follows.
class ErrorsController extends BaseController {
public function __construct()
{
// vacio
}
public function getIndex()
{
return View::make('error.accessdenied');
}
public function getAccessDenied()
{
return View::make('error.accessdenied');
}
}
I have a view with a link to chek if it is working properly. The link is created as follows
{{ HTML::linkAction('ErrorsController#getAccessDenied', 'Error') }}
When I click on the link the page moves to the URL 'mytestdomain.com/error/access-denied' returning an 404 error, but when I access the URL 'mytestdomain.com/error' it works perfectly.
Any idea on what I'm doing wrong?
EDIT:
Running the command php artisan routes these are the routes pointing to ErrorsController:
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| | GET|HEAD error/index/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getIndex | | |
| | GET|HEAD error | | ErrorsController#getIndex | | |
| | GET|HEAD error/access-denied/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getAccessDenied | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE error/{_missing} | | ErrorsController#missingMethod | | |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
Only the sencond and the fourth ones are working.
It looks as though specifying the route in the way you have won't work. This type of routing only works for RESTful requests. See >http://laravel.com/docs/4.2/controllers#restful-resource-controllers>.
You might have to explicitly specify the route using Route::get/post.
Somehow I found the problem.
For some reason, my apache server doesn't rewrite mytestdomain.com/error/ * route. Probably is something related with the word error and the apache module mod_rewrite.
Anyway, defining the route as follows solves the problem.
Route::controller('fail', 'ErrorsController');
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.
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.
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.
i have resource in route and that work correctly and i want to change that to Route::controller.
but after define that i get error in php artisan route :
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| | GET index | index | Closure | | |
| | GET admin/index | dashboard | Closure | | |
| | GET logout | logout | Closure | | |
| | POST auth | auth | Closure | csrf | |
| | GET login | login | Closure | | |
| | GET admin/admin/profile/{_missing} | | ProfileController#missingMethod | | |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
my Current route is:
Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
and i want to change that to :
Route::controller('admin/profile', 'ProfileController', array('index'=>'profile.index') );
how to resolve this problem?
This is not an error, Resource and Controller routes are completely different things.
Resource routes have a predefined list of routes (index, create, store, delete, update). If you don't have the method set in your controller it will still work, unless someone hit that route.
Controller routes relies on your controller methods:
public function getIndex() {}
public function getCreate() {}
public function postStore() {}
Methods names are predefined as
<http method><your action name>()
If those methods are not present in your controller, Laravel will not show them in your routes list.
So, just create a
public function getIndex() {}
In your controller and run
php artisan route
Again.
Use :
Route::resource('profile, 'ProfileController', array('as' => 'profile', 'names' => array('index' => 'profile.index')));
Instead of either the routes above.