In the Laravel documentation on resource controllers (http://laravel.com/docs/controllers#resource-controllers), there is a section titled "Adding Additional Routes To Resource Controllers".
It says to add a route before the resource route is declared. So, in my route.php file I have this:
Route::get('faq/data');
Route::resource('faq', 'ProductFaqController');
After adding the first line show above, my /faq route no longer works. I receive the following error:
Missing argument 2 for Illuminate\Routing\Router::get(), called in /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 208 and defined
Is the documentation wrong? How can I add an additional route to my resource controller? I would like to add a route to /faq/data that will respond to a GET request.
You are missing the action, what should faq/data do?
Route::get('faq/data', function()
{
return 'Hello World';
});
or to a Controller method
Route::get('faq/data', 'MyController#showHelloWorld');
Related
I created the following route
Route::resource('posts','PostController');
Now I'm trying to create a custom route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
When the link posts/trashed is clicked, it goes to the PostController#trashed, but the controller shows blank page without any errors.
So changed the custom route to
`Route::get('post/trashed','PostController#trashed')->name('posts.trashed');
by simply changing the posts/trashed to post/trashed and the controller works fine.
Can someone explain what the issue is.
Figured it out. Solution was hidden deep inside the Laravel Documentation . https://laravel.com/docs/5.0/controllers#restful-resource-controllers
If it becomes necessary to add additional routes to a resource controller beyond the default resource routes, you should define those routes before your call to Route::resource:
Re-ordering will solve the issue.
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Route::resource('posts','PostController');
The resource route has some routes inside it own.
So this route:
Route::resource('posts','PostController');
contains this route :
Route::get('posts/{post_id}',PostController#show)
So it has conflict with your route
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed');
Solution 1:
bring your route upper than resource
I mean:
Route::get('posts/trashed','PostController#trashed')->name('posts.trashed'); <-- first
Route::resource('posts','PostController');
Solution 2:
As you mentioned use another url (like post/trushed)
Solution 3 :
Add exception for resource.
Route::resource('posts', 'PostController')->except([
'show'
]);
I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs
Its Laravel 5.
When the route.php contains this:
Route::get('/foo', function () {
return 'Hello World';
});
then the page shows with the text "Hello World".
However, as soon as I add this new line in route.php:
Route::get('/foo2', 'IndexController');
then the page show this error:
UnexpectedValueException in Route.php line 567: Invalid route action: [App\Http\Controllers\IndexController]
I previously created a controller with artisan which now looks like this:
class IndexController extends Controller
{
public function index()
{
echo 'test';
}
}
what am I doing wrong?
You have to specify wich method will be executed:
Route::get('/foo2', 'IndexController#index');
If you are using get method of Route. Normally first argument provided should be the url and second argument should be the method (there are other ways argument could be passed)
Route::get('/foo2', 'IndexController#index');
If you want to resourceful route . Normally first argument should be the resource name and the second argument should be RESTful controller name. (there are other ways argument could be passed).Example: photo is the resource name and PhotoController is the controller name.
Route::resource('photo', 'PhotoController');
in your case it should work this way
Route::resource('/foo2', 'IndexController');
or
Route::get('/foo2', 'IndexController#index');
so when you visit
yoursite.com/foo2
you will be displayed with IndexController index method
See reference more to learn laravel's restful resource controller
reference: https://laravel.com/docs/5.1/controllers#restful-resource-controllers
You need to specify the function inside the controller not just the controller:
Route::get('/foo2', 'IndexController#index');
You have to reference Controller#method as:
Route::get('/myroute', ['uses' => 'MyController#methodName']);
I've had this working but now a route is no longer found and I can't see why.
In a javascript function I am making an ajax post to the function with this url:
url: '/customers/storeajax',
In my routes.php file I have the following routes:
Route::post('customers/storeajax', array('as'=>'storeajax', 'uses' => 'CustomersController#storeAjax'));
Route::post('customers/updateajax/{id}', array('as'=>'updateajax','uses' => 'CustomersController#updateAjax'));
Route::resource('customers', 'CustomersController');
Now when I try to POST to the storeajax route I get a ModelNotFoundException which to me means the route could not be found so it defaults to the default customers controller show method - in the error log I can see the following entry:
#1 [internal function]: CustomersController->show('storeajax')
confirming its treating the storeajax as a parameter.
I've placed my additional routes above the default resource route
I've had this working before I can't see where I've gone wrong.
In addition these routes are placed in a group:
Route::group(array('before' => 'sentryAuth'), function () {}
which simply ensures user is logged on. To test though I've removed outside the group and at the top of the file but still they don't work.
The url in my browser is coming up correctly as: http://greenfees.loc/customers/storeajax (which I can see in firebug console
I'm using POST as the ajax method - just to confirm
Can anyone see why this route doesn't work and what I've missed?
Update:
Here's the method inside the controller:
public function storeAjax()
{
$input = Input::all();
$validation = Validator::make($input, Customer::$rules);
if ($validation->passes())
{
$customer = $this->customer->create($input);
return $customer;
}
return Redirect::route('customers.create')->withInput()
->withErrors(validation)
->with('message', 'There were validation errors.');
}
I'm 99% certain though that my route is not reaching this method (i've tested with a vardump inside the method) and the issue relates to my route customer/storeajax cannot be found.
What I think is happening is as customer/storeajax is not found in the list of routes starting with customer it is then defaulting to the resource route that appears on the list and thinks this is a restful request and translating it as customer route which defualts to the show method and using the storeajax as the parameter which then throws the error modelnotfoundexception because it cant find a customer with an id of 'storeajax'
This is evidence by the log detailing a call to the show method as above.
So for some reason my route for '/customers/storeajax' cannot be found even though it appears to be valid and appears before the customers resource. The modelnotfoundexception is a red herring as the cause is because of the routes defaulting to the resource constroller of customers when it cant find a route.
A route not being found raises a NotFoundHttpException.
If you are getting a ModelNotFoundException is because your route is firing and your logic is trying to find a Model, wich it can't somehow, and it is raising a not found error.
Are you using FindOrFail()? This is an example of method that raises this exception. BelongsToMany() is another one that might raise it.
I solved this by renaming the method in the controller to 'newAjax' and also updating the route to:
Route::post('customers/new', array('as'=>'newajax','uses' => 'CustomersController#newAjax'));
the terms store I assume is used by the system (restful?) and creating unexpected behaviour. I tested it in a number of other functions in my controller - adding the term store as a prefix to the method then updating the route and each time it failed.
Something learned.
I am getting an error message when trying to register all the controller routes in Laravel 4 (Illuminate) by adding:
Route::controller(Controller::detect());
to my routes.php
The error :
Error: Call to undefined method Illuminate\Routing\Controllers\Controller::detect() in C:\wamp\www\travless\app\routes.php line 13
I suppose they changed the function name, but I don't know where to find it because it is still an alpha version and there is no documentation I'm aware of.
This function has been removed in Laravel 4 because of inconsistent behavior with varying filesystems. The proper way to register controllers should be to explicitly define each one you wish to use in your routes file.
You need to register each controller manualy in routes.php file
Route::controller('users', 'UsersController');
First params stands for URL to respond, second one is controller's class name