How to create a default route - php

I just started with PHP Laravel and i couldn't figure out how to create a default route.
With ASP.NET MVC you could create a default route that would point to the requested controller and action. So you don't have to create a new route for each controller & action.
Is this possible?
Something like:
Route::get('{controller}/{index}', 'HomeController#index');
The HomeController would be the default is no controller was specified and index would be the default action if no action was specified.

You can create resource controllers in Laravel but the exact "default" functionality you are looking for doesn't exist out of the box. Check out the resource controllers:
http://laravel.com/docs/4.2/controllers#restful-resource-controllers
However, this is only really for RESTful routes.
If you want to create it manually then you want to do something like this:
Route::get('{controller}/{action}', function($controller, $action) {
$controller = ucwords($controller).'Controller';
App::make($controller)->{$action}();
});

You can use Route::controller
Take a look at this answer, it may be close to what you want
https://stackoverflow.com/a/18180606/4433967

Related

How to remove default index action on controller from laravel 5.5

I am using Laravel 5.5 version. I defined my routes in the routes.php file. like this:-
$router->group(['middleware' => 'auth'], function($router) {
$router->resource('/route-name', 'myController#myMethodName');
});
But when I run my application laravel gives error:-
Method [myMethodName#index] does not exist on [App\Http\Controllers\myController].
It is by default put index action after my defined action in the routes.
It is working fine in the laravel 5.3 version. Please solve my problem..
Try this as #devk told in comment:
$router->get('/route-name', 'myController#myMethodName');
By Default resource Request create CRUD requests in routes. For the following methods in Controller.
Index(GET)
Create(GET)
Store(POST)
Show(GET)
edit(GET)
Update(PUT/PATCH)
Delete(DELETE)
You can't pass method name in resource route.
If you want to override any of them. you have to write new one route below the resource Route. Like
Route::get('url','Controller#newMethod');
And Change the method name in Controller with newMethod
For more detail check laravel documents

Laravel add custom method to resource controller

I'm using laravel 5.2 and I wanted to know if there's an option to include into the resource more methods.
for example, Id'e like to create a POST method called getUsersList which I can limit the results. I know I can just add in the routes separately from the resource a new route, but I would need to do this for every route I do.
What's the best way to do this?
Of course you can add new actions (methods) to RESTful controllers.
Just add method and create the route for this action:
Route::post('foo/bar', 'FooController#bar');
And don't forget to put this route before RESTful route:
Route::post('foo/bar', 'FooController#bar');
Route::resource('foo', 'FooController');

Create a single route for every controller and method in Laravel

Can I set a default route, so when I have a request like /home/index and /home, it would redirect to the home controller's index action, just like in other frameworks?
I know that I can set them one by one but I don't want to add a route for every request, I would like to use only one route for all requests.
Theres two other types of controllers beside the basic controller. You can create these by specifying a special route to your controller. With this technique you don't have to create a route for every method just one per controller.
Resource Controller
This will create all the methods with the corresponding HTTP verb you need for managing one resource like a user or a product. There is a table in the documentation that contains which predefined route matches the predefined methods of the controller, that represents an action you can do with a method, like edit, create, destroy, etc:
Anyway, you still free to add extra methods and routes beside the resource controller methods and routes, just keep in mind that you have to do this before defining the resource controller route:
//Extra route for the resource controller.
Route::get('home/profile', 'HomeController#profile');
//Resource controller routes.
Route::resource('home', 'HomeController');
RESTful Controllers
I think this is what will fit better for your needs.
Creating a RESTful controller will automatically create a route for all methods that begins with a HTTP verb.
Route::controller('home', 'HomeController');
After this, you can create methods like these in your HomeController:
public function getIndex() {
//Code.
}
public function postProfile() {
//Code.
}
The framework will automatically create the routes for them, so you can access postProfile() through a HTTP POST to the /home/profile route. Also you can access getIndex() through a HTTP GET to the /home/index.
The documentation also mentions:
The index methods will respond to the root URI handled by the controller.
In our case that means that you can acces your getIndex() method through the /home/index and the /home routes too.
If you have a method that has multiple words in it (a word start with a camel case letter), then the generated route will have a - between the words, so the method getAdminProfile() will have a route called home/admin-profile.
Also as I told at the resource controller section, you can still create regular routes, just be sure to create them before you create the RESTful controller's route.
Final answer
Create a route: Route::controller('home', 'HomeController'); call your root method getIndex() and prefix every other method with a HTTP verb e.g. userTool() should become getUserTools().
If you're using Route::controller() just name your index() method getIndex().

Some Routing problems in laravel 4

I am new to laravel 4 framework but was previously working on CI and CakePHP, i have some problems with routes in it (i may sound nerd, so bear with me.)
-> If i have 3 controller userController,adminController,editorController and many methods inside them, do i need to define routes for every methods inside it (ofcourse i am not using ResourceFull controller for them). Can't i have something by which the methods can be accessed by using the controllername followed by method name like we do in other frameWork.
E.g usersController have manageUser method, i wnt to access it like
http://localhost/project/users/manageUser
-> What is use of defining a route using Route::controller('users', 'UserController'); or restfull controller?
Thanks in advance :)
If you write
Route::controller('users', 'UserController')
runs the default function (index of all the objects), but you can write:
Route::get('/users', 'userController#function');
or
Route::post('/users', 'userController#function');
this route shows to Laravel what controller and function can call when you write this route, the diference is if you pass the parameters with get or post mode.
Hope I help you

Using any other controller class rather than the default controller class

I am working with CodeIgniter. Here's my routing file
$route['default_controller'] = 'pages/view/home';
$route['(:any)'] = 'pages/view/$1';
where
pages is the controller class and view is a function of it and home is a parameter to that function.
Now, this is the default controller. When i need to open someother page rather than 'home' I do it like as follows from inside a view
href="<?php echo base_url('products');?>
Now what i want to ask is, if i create a new controller, how can i use the function of that controller? since I am only passing the third parameter to the base_url() function.
Obviously I think I gotta write $routes, but how ? since all the traffic is passed to
pages/view
I tried creating a new controller but couldn't be able to use it. My new contoller was name new_controller and it has a function call new_function()
and I wrote the $route as follows
$route['pages/view/product'] = 'new_controller/new_function';
You shouldn't have to worry about Routes if you take away the (:any) route you have place there. That is blocking all other controllers from being loaded, I think.
If you have a controller called "Stuff"
in your URL when you have mysite.com/stuff/foo/param Code Igniter should bypass the default "page" controller and use
I think you would be better off doing something like this
$route['page/(:any)'] = "page/view/$1";
And change your default to be only 'pages'
That would open up your new controller to be used in the normal codeigniter fashion
In CodeIgniter the routes are evaliated in row, so first you have the default route and after that sould you place the new route, $route['pages/view/product'], if you want to keep the (:any) route, and with this, you place the exceptional routes before the (:any) route.

Categories