I'm new in laravel and I got an error and don't really know how to fix it.
I got an error "Controller method not found" when i'm asking for this route : /projet/6/note
My routes.php
Route::controller('projet.note', 'NoteController');
Route::resource('/eleves', 'StudentController');
Route::controller('/auth', 'AuthController');
Route::resource('/user', 'UserController');
Route::resource('/projet', 'ProjectController');
Route::post('/eleves/search', 'StudentController#postSearch');
Route::resource('/classe', 'ClasseController');
Route::controller('/', 'HomeController');
I tried to type php artisan routes to see if the routes was working, and she's not.
I tried then to change controller into resource in the line about NoteController, the routes was there but when i go on the link, same error.
Then i guess i can't do 'projet/note' without that my NoteController is a resource?
It's a problem because i need to nest NoteController to ProjetController.
My only action in NoteController
public function getIndex($id)
{
return View::make('note.noter')
->with('project', Project::find($id))
;
}
Thanks
I hope I understood your question right. This is the best solution I could come up with:
Route::any('projet/{id}/note/{action?}', function($id, $action = 'index'){
$controller = App::make('NoteController');
$action = strtolower($_SERVER['REQUEST_METHOD']).studly_case($action);
if(method_exists($controller, $action)){
return $controller->callAction($action, [$id]);
}
});
This basically does some similar things as Route::controller. First we create a controller instance, then build the action name out of the request method and the second parameter in the route and in the end, call the action (if it exists).
Related
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
I have a single domain/subdomain project. In order to see the event by slug, I made this route:
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
After my page didn't load correctly, I did a dump in the controller:
public function getView($slug)
{
return $slug;
}
To get to the route I am using this URL: https://example.com/events/slug-example.
The problem is that the route is being hit as I see the response when I change it, but I am not getting the slug, instead I am getting Region object back.
If I do this:
public function getView($region, $slug)
{
return $slug;
}
Then I get the slug back. But I have no idea how is this possible, and how could I do it (I came as another dev on the existing project).
I tried commenting out all the middleware and it is still the same. How can I even make something fill the method if I didn't explicitly say it?
EDIT
I noticed there is binding going on in routes file:
Route::bind('region', function ($value) {
...
});
Now if I dd($value) I get the variable back. How is this value filled? From where could it be forwarded?
Looking quickly it should work, but maybe you was verifying other url.
Make sure you put:
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
routes at the end of routes you showed.
EDIT
If you think that's not the case and you don't have your routes cached you should run:
php artisan route:list
to verify your routes.
EDIT2
After explaining by OPs in comment, domain used for accessing site is:
{region}.example.com
So having $region in controller as 1st parameter is correct behaviour because of route model binding and other route parameters will be 2nd, 3rd and so on.
Instead of
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
try
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
});
Any help why this is not working ,I am using Laravel 5.5.23 version ,this is are my routes :
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads','ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('threads','ThreadController#index');
Route::get('threads/{channel}','ThreadController#index');
Route::get('threads/create','ThreadController#create');
Route::get('threads/{channel}/{thread}','ThreadController#show');
Route::post('threads','ThreadController#store');
Route::post('/threads/{channel}/{thread}/replies','ReplyController#store');
Route::get('/logout' , 'Auth\LoginController#logout');
This is the ThreadController ,just the relevant methods actually :
public function __construct()
{
$this->middleware('auth')->except(['index','show']);
}
public function index($channel = null)
{
if($channel){
//do something
}
else{
$threads=Thread::latest()->get();
}
return view('threads.index',compact('threads'));
}
The problem is when I try to access /threads/someChannel it returns not found ,so this is the problematic route : Route::get('threads/{channel}','ThreadController#index'); ,all other routes are working ,any idea why this one is not working ?
The Route::resource('threads','ThreadController') call is defining 7 routes with the prefix threads. Some of the routes you are defining yourself are being masked by this. Check php artisan route:list to see what routes the Route::resource call registers for you first. First come first serve when it comes to matching routes.
/threads/someChannel is going to match the route defined by the resource call:
GET /threads/{thread} ThreadController#show
Since you don't have the definition for show which is relevant to have, I would assume you have Implicit Model Binding happening. It is trying to bind that model to that parameter and it can't find it in the database by that ID and is causing a 404 because of it.
I use :
Route::controller('home', 'HomeController');
in my routes to link all routes to my controller.
I have a getIndex() function in my controller that's executed when I go to '/home'.
I have a case where I'd like to route to '/home/slug', but not always.
I tried using getIndex($slug), but it always asks for '/home/index/{slug?}'. I don't want index to appear.
Not possible using implicit controllers, as far as the documentation goes (as it seems to enforce RESTFUL pattern).
But your can create a new route just for that:
Route::get('home/{slug}','HomeController#slugedIndex');
Route::controller('home', 'HomeController');
Edit: as pointed by Steve the controller method must come after the get method so one does not overwrite the other.
Arthur's answer was :
Route::get('home/{slug}','HomeController#slugedIndex');
Route::controller('home', 'HomeController');
Although it doesn't work, because anything written after 'home/' will now go into the first route (and HomeController#slugedIndex).
I found a workaround though. I took out the route in routes.php :
Route::controller('home', 'HomeController');
Then in my HomeController, I used the missingmethod() that's called whenever a method isn't found in the controller.
Here's the missing method :
public function missingMethod($parameters = array())
{
$sSlug = is_string($parameters) ? $parameters : '';
$oObject = Object::where('slug', $sSlug)->first();
if ($oObject) {
// slug code
}
else {
// 404 code
}
}
At first it seems that the same routes. But in first route not working middleware that I ordered in the constructor.
How to fix that?
Route::get('/cars.get', function() {
return App::make('App\Http\Controllers\CarsController')->{'get'}();
});
Route::get('/cars.get', 'CarsController#get');
sorry for my English =)
Edit
I was wrong about callAction() it does nothing else than call the method.
Unfortunately there doesn't seem to be a simple API to call middleware manually. A solution to this would just be to define the middleware on the route:
Route::get('/cars.get', ['middleware' => 'auth', function() {
return App::make('App\Http\Controllers\CarsController')->{'get'}();
}]);
original answer:
By directly calling the get() method you skip middleware defined in the controller. You should use callAction() instead:
return App::make('App\Http\Controllers\CarsController')->callAction('get');
Also note that you can use app() as a shortcut for App::make():
return app('App\Http\Controllers\CarsController')->callAction('get');