Laravel 5.1 Using controllers as a route - php

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.

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.

Create pages without using routes.php in Laravel

I have been using Laravel since version 3 and have also routed each URL I created to routes.php. However, I have seen some projects that don't use routes.php at all but still have URLs (of course). An example is Octobercms. So, my question is how is that even possible? I see zero indication of routing the way Laravel states in their doucmentation
When you look at a conventional routes.php you see a lot of Route::get() Route::resource() etc...
Those calls register the routes through the Route Facade. Now the thing is, you can register your routes everywhere you want (Route has to be accessible and it has to be "early" enough)
For example, if you're developing a package that only has one or two routes, you might want to do that in the boot function of your service provider.
But what Laravel does with the routes.php, it simply includes it with require
vendor/laravel/framework/src/Illuminate/Foundation/start.php
$routes = $app['path'].'/routes.php';
if (file_exists($routes)) require $routes;

Laravel optional routing parameter

I am trying to build up my routes file and for the application we are building we may have a route such as:
/services/{game}/{id}
Is there a way to have that {game} parameter, but not actually pass it to the controller? Its basically there, but doesn't have to be anything specific and the controller doesn't need to know its there, it's purely for the users eye to make their URL make sense.
That is perfectly possible, you can catch it in the controller without doing anything with it, however you will have to catch it.
Do what Bielco said or just minify your route using slugs instead 2 parameters.
For example:
Game: Skyrim Legendary Edition
Slug (unique): skyrim-legendary-edition
Your route: /services/game/skyrim-legendary-edition
In Laravel routes.php
Route::get('services/games/{slug}', 'ServicesController#showGame');

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

Categories