Laravel 4 understanding resources - php

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.

Related

Laravel Route Controller

I have a working solution within routes.php, but I understand that laravel can handle restful routes better. I've tried using their documentation to implement restful resource controllers, but had no luck.
This is what I have at the moment
Route::get('/invoices', 'InvoicesController#showInvoices');
Route::get('/invoices/data', 'InvoicesController#getInvoices');
Basically, the showInvoices returns the invoices view and getInvoices returns a JSON string for DataTables which is called from the invoices view.
So I want to be able to call /invoices to get the view and then call /invoices/data using JavaScript.
Any suggestions how to convert this to a resource controller or more suitable controller?
Yes, there was a cleaner way. Route controllers were supported up to Laravel 5.3. Then this functionality was removed in favor of explicit routes, which leave the routes files in disarray when you have lots of routes.
Fortunately, there is a class I wrote called AdvancedRoute, which serves as a drop in replacement.
In your case you can use it like this:
Route::get('/invoices', 'InvoicesController#showInvoices');
Route::get('/invoices/data', 'InvoicesController#getInvoices');
Becomes:
AdvancedRoute::controller('/invoices', 'InvoicesController');
Explicit routes are built automatically for you. Have in mind you have to follow a convention by prefixing the method names with the request method, which I personally find very clean and developer friendly:
InvoicesController#getInvoices => /invoices
InvoicesController#getInvoicesData => /invoices/data
Full information how to install and use find at the GitHub repo at:
https://github.com/lesichkovm/laravel-advanced-route
Hope you find this useful.
You could create a "resource" route like so:
Route::resource('/invoices', 'InvoicesController');
Which will provide RESTful routes (GET, POST, PUT, etc...) for that particular /invoices route/resource. You can check this by executing php artisan route:list
You can learn more here.
I hope this helped.
Cheers!

Laravel RESTful controller add parameter to create

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.

Avoiding duplicate code in two controllers in Laravel 5.1

I have a route in Laravel 5.1 that will accept a generic permalink and will need to determine what object it belongs to (for example is it a permalink for a "Blog" or a "Story"?).
The route looks like this:
// .... Every other route in the routes.php file //
Route::get('{generic_url}', 'CMSController#generic');
Then the code in the my controller looks like this:
public function generic($generic_url) {
$blog = Blog::where('permalink', $generic_url)->first();
if(!is_null($blog)) {
// Load a blog entry page
}
// Something basically the same as above but for Story
}
I also have this route in my routes.php file to view a blog post:
Route::get('/blog/{blog_id}', 'BlogController#view');
The purpose of that second route was for me rough in the view a blog post page as well as a quick way for me to debug a particular post.
I am hoping to avoid having to put view code in two separate controllers. My first thought was to try and find a way to have CMSController call the view action in the BlogController. It sounds like a terrible idea to me and some searching around confirms that it is a terrible idea.
My question is, what is the best way to handle this situation?
For the time being I decided to add a static method to my Blog model and place it in the controllers:
return Blog::RenderFrontendView($blog_id);
I know this seems a bit strange considering I have already loaded the model in one of the spots I am using this code, but in the context of what I am doing it makes sense. It looks in the cache to retrieve all the info and I intend to do further optimization to only load the ID instead of the entire model.
Update: I am no longer using this method as using a trait was a much better solution

Laravel dynamic rerouting

Hey guys i have a little problem i need to solve and i cant seem to find a way to do so.
I have an app that need to use different databases dynamically according to which user uses it. I thought that i would give each user an URL that contains hes unique alphanumeric id. So the URL would be something like ww.mydomain.com/app/1kh1h3as/
So i have 2 problems:
where should i put the database switch code. Is it better of in config file or should i use it in model classes so i have use of URI class to parse out the id?
how can i make the router understand that it needs to offset all the calls by one segment so it wont go looking for 1kh1h3as controller and ww.mydomain.com/app/1kh1h3as/users/all will launch all() method within Users controller?
Use this
DB::connection('mysql')->config['database']='your_user_database_name';
Try
Route::any('app/1kh1h3as/(:bundle)', function() {
return 'Welcome to the Admin bundle!';
});

Removing Controller name from URL in CodeIgniter

Currently in my CI project I have a single controller that handles all things account. Such-as register, login, activation, etc.
My routes work as such...
domain.com/account/login/ or domain.com/account/register/
How can I remove account from the route while also being about to remove the controller from other pages.
I basically want the controller to always be removed. One of my reasons for this is SEO, search engine rank the importunateness of a page based on how deep it is in a website.
The only way I have seem to achieve this is to do some thing like route['activate'] = 'account/activate'; for every single page, which would be a huge hassle.
$route['^(?!other|controllers).*'] = “account/$0″;
Try this :
$route['(:any)'] = "account/$1";
The answer to your question is that you DO have to explicitly set the routes.
How is it going to know which controller a given function is in????
You have to tell it.
use mod_rewrite (if the controller is always the same name)
Ok, I can think of one way to do this, but it is probably gonna be more of a pain than just writing out routes for each function.
You need to extend the Router.php with application/core/MY_Router.php and overide the _validate_request() method. Which basically decides if this this is a valid route or not.
it does a check to see if the controller class exists then fails if it doesn't exist.
You need to replace this with some code which assumes no controller segment, then scans thru each of your controllers and checks if it contains the method called (it will be segment 1, since theres no controller).
Now the tricky part, at this point in the CI lifecycle your controller obviously isnt loaded, so you cant examine it using method_exists() yet.
You need to load your controllers one at a time, and then for each one run
method_exists($loaded_class, $method_name)
and if its true, then set then go ahead and call:
$this->set_class('the_name_of_the_scanned_class_which_had_the_method');
Then CI can keep going on as normal and it will load your methods without the user ever know what controller it loaded from.
.. probably not worth the hassle imho. A much easier solution would be to just have one controller and one route to that controller.

Categories