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().
Related
I am new to Laravel and I am trying to make a function to create a database row but my routes are not working.
I currenlty have this in my web.php file:
Route::get('/admin/pagina', [PaginaOverzichtController::class, 'index'])
->name('Admin_Pagina_Overzicht')
->middleware('auth');
Route::post('/admin/pagina', [PaginaOverzichtController::class, 'CreatePage'])
->name('Admin_Pagina_CreatePage')
->middleware('auth');
Route::post('/admin/pagina', [PaginaOverzichtController::class, 'DeletePage'])
->name('Admin_Pagina_DeletePage')
->middleware('auth');
But when I go to /admin/pagina, I get a Route [Admin_Pagina_CreatePage] not defined error.
Am I allowed to have the same URL but different name pointing to a different function in the same controller? If not, is there a best-practice way to do this?
^
I have a form on my page that should create a page with the method post and action {{ route('Admin_Pagina_CreatePage') }}
Am I allowed to have the same url but differend name pointing to a differend function in the same controller?
No. When determining uniqueness in Laravel routes, the HTTP Method & URI act as a combined primary key. Everything else is just metadata attached to that unique entry.
In your example, the second Route::post('/admin/pagina') is overwriting the first one, because you've defined the same "ID" pair of POST /admin/pagina.
I'm not sure how you expect to have the same HTTP method and URI go to two separate controller actions. If you expect to route them differently based on what's included in the request body, that conflicts with how Laravel's routing works (routes are found and dispatched without usage of the request body).
Normally for a delete, you would utilize that HTTP method in the routing:
Route::delete('/admin/pagina' [/* ... */]);
This can be paired with form method spoofing to trick a normal form request (which doesn't support DELETE) to find that appropriate route anyway.
Not related to your problem it's just a suggestion, you can use Route Groups and assign a middleware to that group to avoid repetition of assigning one middleware to each route.
E.g.
Route::middleware(['auth'])->group(function(){
Route::get('/admin/pagina', [PaginaOverzichtController::class, 'index'])->name('Admin_Pagina_Overzicht');
Route::post('/admin/pagina', [PaginaOverzichtController::class, 'CreatePage'])->name('Admin_Pagina_CreatePage');
Route::post('/admin/pagina', [PaginaOverzichtController::class, 'DeletePage'])->name('Admin_Pagina_DeletePage');
});
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');
I'm just new to Laravel but I immediately fell in love with it. As a not so super experienced php developer I do find the official documentation, although very expansive, somewhat complicated to use and find everything I need.
My question is about the Routing component. As the documentation states you can assign a route to a controller with the Route::controller method. So if I want a Blog controller for all /blog/ routes I assign it like this:
Route::controller('blog', 'BlogController');
So then if I'd like to acces all my blog posts I acces the the getIndex method by www.foo.com/blog or www.foo.com/blog/index
But let's say I'd like to be able to display categories via a getCategory method. My url would look like www.foo.com/blog/category and if, for example, I want to get the news category from the DB by slug, I'd like to use: www.foo.com/blog/category/news as the URI.
My question now is, how do I pass the slug to the url and access it in the getCategory method? Do I need specify it via Route::get('blog/category/{slug}', 'BlogController#getCategory') or is there a way to use Route::controller('blog', 'BlogController') and to send and acces parameters from the URL in the getCategory method?
I already tried to find it via google and in the official documentation, but I couldn't find a crystal clear answer to this problem...
You can simply add parameters to your getCategory method:
public function getCategory($category) {
die($category);
}
If you initialize it to null in the parameter list, it becomes optional. Alternatively, you can always pull parameters from the Input object but they would need to be passed in querystring format:
$category = Input::get('category');
With that said, I'd caution against using the Controller route. It's handy and mimics traditional MVC frameworks, but I believe it's planned to be deprecated -- and honestly, you miss out on some pretty flexible features.
using Route::controller('blog', 'BlogController'); allows you to define a single route to handle every action in a controller using REST naming conventions.then you have to add methods to your controller, prefixed with the HTTP verb they respond to. That means if you have a method called getIndex() it will be executed when there is a GET request to the url "yoursite.com/blog".
To handle POST requests to the same url add a method prefixed with post(ex: postComment()) and so on for other http verbs PUT, PATCH and DELETE.
I think you want something more customized, so you can use a resource controller:
Route::resource('blog', 'BlogController');
This will generate some RESTful routes around the blog resource, run php artisan routes in your project folder to see the generated routes, it should be something like this:
Verb Path Action Route Name
GET /blog index blog.index
GET /blog/create create blog.create
POST /blog store blog.store
GET /blog/{blog} show blog.show
GET /blog/{blog}/edit edit blog.edit
PUT/PATCH /blog/{blog} update blog.update
DELETE /blog/{blog} destroy blog.destroy
in the action column are the functions that you should have in the controller.
If you want to define more routes you can simply do it with Route::get or Route::post in the routes.php file
I hope this will make it more clear for you, enjoy routing with Laravel!!!
Laravel routing functionality allows you to name a resource and name a controller to go with it. I am new to Laravel and would like to know if anyone knows how to extend the resources method in the route class provided.
Basically say I have: (which works fine)
/invoices
But say I want:
/invoices/status/unpaid
How is this achievable?
To see the basics of what I am doing check:
http://laravel.com/docs/controllers#resource-controllers
Resource controllers tie you into a specific URLs, such as:
GET|POST /invoices
GET|PUT /invoices/{$id}
GET /invoices/create
and so on as documented.
Since, by convention, GET /invoices is used to list all invoices, you may want to add some filtering on that:
/invoices?status=unpaid - which you can then use in code
<?php
class InvoiceController extends BaseController {
public function index()
{
$status = Input::get('status');
// Continue with logic, pagination, etc
}
}
If you don't want to use filtering via a query string, in your case, you may be able to do something like:
// routes.php
Route::group(array('prefix' => 'invoice'), function()
{
Route::get('status/unpaid', 'InvoiceController#filter');
});
Route::resource('invoice', 'InvoiceController');
That might work as the order routes are created matter. The first route that matches will be the one used to fulfill the request.
I am confused with codeigniter routing. I am implementing URL masking in my project by using router in codeigniter.
From this I got confusion about the routing.routes has given below.
$route['project/shareToFacebook/(:any)']="project/shareToFacebook/$1";
$route['project/shareToFacebook/(:any)']="project/profile/$1";
My question is when I called the controller shareToFacebook what does route will do?
whether controller profile will be invoke or the controller shareToFacebook will be invoke?
Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
CodeIgniter user guide: Routing
You will always be sent to shareToFacebook, but you will be sent to any of these routes only if you pass some parameters so when you will call the controller it will open it's index method regardless, if you won't pass any arguments.
when I called the controller shareToFacebook what does route will do?
The method 'shareToFacebook' will accept 1 parameter ex.
if you call ex. localhost/yourproject/profile/shareToFacebook/1 <- will be passed on the method shareToFacebook
public function shareToFacebbok($value)
and you can do what ever you want with that value.
whether controller profile will be invoke or the controller shareToFacebook will be invoke?
No.
profile will still call its index method