Laravel RESTful controller add parameter to create - php

I am trying to use Laravel 5.2's RESTful resource controllers. However, when moving from my index to create, I would like to pass a parameter as the create page should be partially filled in.
edit
The form that will be 'created' will have populated fields from the database already. So the create should take the id from the user that is clicked in the index.
My temporary solution:
Route::get('consultation/{id}', 'ConsultationController#create');
Route::resource('consultation', 'ConsultationController', ['except' => ['create']]);
Is there a way to add this to an options array in the same line as resource?
Thanks
Edit: I suppose in this case, my store would also need the same {id} parameter.

I was also curious about that but I think that the Laravel Documentation is pretty clear about this:
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
And they add the following, meaning for me that if you want to override a defined route, you just need to put the definition on top of the Route::resource() definition.
otherwise, the routes defined by the resource method may
unintentionally take precedence over your supplemental routes
EDIT
After better understanding the question, I would let the restful controller as is, and create a new route like /user/{user-id}/consultations/create that is much more "restful" like.

Related

Laravel 5.1 Using controllers as a route

I use the Laravel 3 and I'm start a project using Laravel 5.1, a lot of things changes, include some way that I use to make my routes.
I like to implemente my routes using my controllers, like the code below:
Route::controller("Search");
Route::controller("Contact");
Route::controller("Login");
Route::get('/', "Inicio#index");
But it's not works in Laravel 5.1, someone knows if this way changes or not exists more?
Thank you.
You can use Route::resource() method, the first parameter will be the URL prefix and 2nd will be the controller name, like this:
Route::resource('admin-panel', 'AdminController');
Now, say if your controller have a method named login, the URL generated will be admin-panel/login. There's also an optional 3rd parameter, check laravel docs for details.

Symfony2 - What is the point of specifying your own route names?

Currently learning Symfony. Just wondering about route names, which the book doesn't really seem to say in advance what they are for.
As I understand it, #Route annotations are given a default name based on the bundle, controller and action name. So what's the point in specifying your own route names?
Is it good practice to either leave all routes to have a default name?
Is it better practice to specify your own name for every route?
What if two routes have the same name?
How am I expected to know if I'm using a route name which is already taken, perhaps in another bundle?
Route names are important because your code will often need to build url's. The route name is how you specify which url to construct. Names are also handy for listeners (such as authentication) which process specific url's.
Annotation generated default names are fine but tend to be long and could change on you.
Yes, I would give every route a custom name for convenience and readability. Plus, I don't use annotations. To me at least, storing routes in a central file make the code easier to maintain. It avoids the need to search through multiple controller files trying to determine which code handles which request.
Routes with the same name will replace any previously loaded route. It's useful if you wish to override a route from a third party bundle.
Namespace your route by using some form of your bundle name as a prefix.

Laravel 4 understanding resources

So, I'm starting to learn L4, but having a few issues. I'm trying to read the documentation on their website, but it seems to be incomplete, or not well done (maybe i'm too newbie at this point). However.
What I'm trying to figure out is, when I've created a resource, and added a route like this:
Route::resource('users', 'UsersController');
how would i create a form, so when i submit something inside "laravel/public/users/create" the method "store" would get triggered? do i need to create a own route for this? would i use the PUT? how should me ACTION URL look like?
Is there any one that can create a good example for me on this one? On how the view / controller would look like?
This page should have everything covered for you: http://laravel.com/docs/controllers#resource-controllers
The "Actions Handled By Resource Controller" is especially helpful. In your example, you would make a class called UsersController with a postCreate() method which will automatically be called when a post request is sent to /users/create.

Use automatic controller routes in Laravel is a bad idea

I'm coming From CodeIgniter to Laravel.
So, is a bad idea using automatic routes to all of controllers?
Route::controller(Controller::detect());
Should I use this instead creating routes in routes.php?
Yes this is bad.
Controller::detect() is actually not present in Laravel 4 because it is a bit broken.
detect() will go through your filesystem and return controller files, but this is a bad idea because the order you define your routes matters. If you have any nested controllers you will find this breaking very easily.
detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.
I would argue that you should define all your routes any ways, it is a lot easier to read and debug.
One of interesting things about Laravel that CI does not have is that for certain pages, you can route directly to the view without needing a controller at all. Think about static pages like 'About Us'. CodeIgniter would need you to set up a controller + view for that, even though the controller will do barely anything. In case of Laravel, you can route directly to a view in this case.
Setting up routes manually will allow you to set these short-circuited routes.
Automatic detection is a bad idea.
You can use routes or use Route::controller('mycontroller') or and array of controllers like Route::controller(array('mycontroller', mycontroller2');
Then you get the benefit, without the autodetect.
in laravel 4 :
you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers
But
Route::controller() must take two parameters as minimum requirement
first parameter stands for URL respond to ,and second parameter is name of controller
also
you can write third parameter into Route::controller() is an array with names of actions (name of action with HTTP verb )and routes names for this actions
ex:
Route::controller('users','UsersController',array(
'getUsers' =>"listUsers" ,
));
route name for getUsers action is listUsers
Below is a good example to follow for CRUD and general purpose routing
type php arisan controller:make SampleController
edit routes.php and add
Route::resource('sample', 'SampleController');
Then type
php artisan routes to show the newly created routes

CakePHP 2.0 Disable Routing Issue

I have my own routing rules in routes.php, defined for all the pages that should be accessible via URL, such as mywebsite/blog/ and mywebsite/blog/category/category-name, i.e. the structure of my whole website is covered by my custom routes.
Now, I have a lot of elements that make use of requestAction, such as
$websiteabstract = $this -> requestAction(array(
'controller' => 'assets',
'action' => 'displayHomeAbstract'
));
This gives me an error Error: Controller could not be found, probably because I have not defined a route for /assets/displayHomeAbstract. But why do I have to define a custom route for that, when I explicitly state the name of the controller and the action? Shouldn't that bypass the routing altogether?
Either I have not understand Routing at all. Or do I really have to define ALL the possible routes (even those that are only used by requestAction) in my routes.php? I mean, I don't want to allow users to directly access mywebsite/assets/displayHomeAbstract anyway, only via an Element.
Thank you
EDIT: Here is my routes.php http://pastebin.com/aAKBwNZJ
Please have a look at line 128, this is exactly what I do not want since /assets/displayHomeAbstract is ONLY accessed via requestAction.
EDIT: And this is the element, that makes the request: http://pastebin.com/0tK5dYJk
Okay, after extensive discussion with the devs in IRC, I think I understand this well enough to explain to you:
You do have to define your custom routes for your requestAction in this case. requestAction is emulating a full request. It dispatches a request as if accessed using the string url every time, even when the url provided is an array. The book is referring to how when you have a custom route defined in addition to using the default routes (the last line of routes.php), you can use array urls to be agnostic of those routes. However, these array urls rely on the default routes.php in the /lib/ folder and are used to construct a url string. If you're going to have a custom routing pattern, you have to construct the url strings on your own.
Note: the comments below were from earlier versions of this answer.
The key to your problem is understanding the scope of Cake's routing and how it work.
When you define a route in CakePHP, it isn't just used for mapping URLs to controllers. It's also used by the Router for things like generating link addresses and, in your case, mapping the path supplied to requestAction() to a controller. Behind the scenes, Cake is creating a URL string based on your parameters, and then passes it off to the Router to find the correct controller. Since no such route exists, it fails.
As a solution, I would actually recommend not using a controller for that logic. Depending on what it does, a component or a helper may be a better place.
Look at line 156. You commented out the line that loads CakePHP's default routes.

Categories