I am using laravel 5.4 and there is one thing i cant understand compeletely. I have used make:controller (name) -resource to create a controller with index/show/create/edit/update/destroy. The app i am making is mostly CRUD operations. My question is :
I must have a separate controller for every entity of my database that needs CRUD operations? For example i have 2 entities : Items, Services. I must have 2 controllers or they can be on the same controller like :
public function store_item(Request $request) {
**Insert Query**
}
public function store_service(Request $request) {
**Insert Query**
}
What is the correct way to do this?
They can be in the same Controller, but It is Good Practice to Create Different Controller for Items, Services or any CRUD Operations when it comes to Laravel.
In can you just want to create one Controller for both Items and Services, you have to make Functions like store_item and store_service And you have to call them With Every route Like Route::post('items','YourController#store_item'); and Similar for Service.
But if you create Separate Controllers for Items and Services, You Don't have to create Route for Every Task or Action. You can just Register Resourceful Route in Your Routes File and you are good to go!
For example, If you are creating Separate Controllers for both Items and Services, You can just write these two Lines in your Routes File and you are good to go!
Route::resource('items', 'ItemsController');
Route::resource('services', 'ServicesController');
So, It's good to Create Controller for CRUD operations because it makes stuff Neat and Clean in Laravel. Let me know if you have any more queries!
Related
I find myself writing same code for my resourceful controllers. Is there is a way to extract the the functions index(), create(), update(), show(), edit() & destroy() into a generic file ? Also, if we do that, will it give me an option to override the those functions in the respective controller?
All you need is a crud generator package like nvd crud generator. It creates working templates for controller, model and views which you can edit yourself to suite your needs. This solution will save you the trouble of writing the same code again and again with the flexibilty of customizing each controller.
However, if you need to have a generaic controller which you can extend from, you can do that as well. You can create a controller ResourceController with artisan make command and then extend all other resource controllers from it.
I have started a new laravel website project and I have hit a road block in my understanding of MVC. I need to refactor to continue but I don't know the best way.
Currently I have web pages that display the results of a single bit of logic. I.e. A page listing all users, a page listing the details of just one user etc etc - all handled by a userController. This applies to other pages being handled by other controllers.
I have created models directly relating to the tables in my database, and controllers in relation on the models. I moved the business logic from the controllers to services. The controllers use the services to perform the business logic and with the data returned, pass that data to the views.
This nicely groups similar functionality together and works fine.
userTable -> userModel -> userController -> userService
clientTable -> clientModel -> clientController -> clientService
...
In my routes, I have pages which do related functionality use the same respective controller, but individual methods depending on what the page does
/listallusers -> userController#list -> userList.blade.php
/listallclients -> clientContoller#list -> clientList.blade.php
/listdetailofoneclient -> clientContoller#details -> clientDetails.blade.php
This is ok when dealing with pages that do that functionality and (apart from using services) seems to be what is hinted at in the laravel docs.
However, I'm starting to get confused about controllers when dealing with pages that either don't really use functionality from any of the services or pages that heavily require functionality from multiple services (and the data needs complex manipulation like formatting or such).
A basic index page.
What controller would handle this? The index might link to routes that are handled by existing controllers but it probably won't need to display much functionality apart from that. That means the controller won't need to pass much complex -if any- data to the view. You could stick the logic to return the view in the route file but that is pretty tightly coupled.
A page that shows complex client and user data
You need to pass client data and user data to the view from the controller. But from what controller? This is the part that is really holding me back.
Because I have a limited number of pages but lots of logic displaying on each page, I was thinking of making a page controller (or something) which would handle the routing. Although I have looked, I have not seen any real mention of this idea anywhere which makes me think I am either reinventing the wheel or have failed to grasp some basic concept in laravel / MVC.
Would the page controller in this case handle all the routing? Would it handle only the pages with 'overlapping' existing controller functionality and the pages that don't fall into the existing controllers? Is a page controller even a good idea?
Some more points that have made me question MVC from this issue
Do controllers need an equivalent model?
Can controllers 'control' other controllers in order to separate logic?
I'll take a shot at a couple of these questions.
Having a PageController or HomeController is very acceptable. Controllers don't have to be linked to a specific model, for instance an AuthController would handle logic for logging in and out but isn't tied to a model, or PasswordController which handles setting/resetting passwords, or a PaymentController that handles billing routes. A controller is just a way to organize logic for related routes in one file. Static basic pages are related routes so a PageController makes sense to me.
Are users tied to a specific client? If so you can arrange your controllers in a nested way so you have a ClientController and a UserController and your routes look something like this:
/clients/{client}/users //list all users for this client
For heavy data formatting it's probably best to use a service provider and use dependency injection to inject it into controllers where you need to use it. This allows you to detach your data manipulation from your controller so you could change it out if needed. Say you are using a software to make charts, and you want to change it out later - you want that formatting logic to be removed from your controller.
I hope I helped in some way... sort of a train of thought here!
Can we wrap multiple model updates in single transaction for a cakephp Controller action.
I found these links which solve this issue.
CakePHP 2.3.x database transaction
How to put begin-commit transaction in controller: cakephp?
But both of them requires code in specific controller-action. Can above solutions be centralized somewhere, so that single global transaction is automatically available to all controllers & actions in application. I'm not able to visualize the solution.
I'm know sure if i got your question right.
I assume you want a specific action in all controllers?
You probably could implement it in AppController like
public function myGlobalAction(){
//dosomemodelstuff();
}
I'm using Laravel web framework for a PHP project. I have a question about this framework, the documentation (which is poor), and the community from IRC doesn't helped me.
I want to create a controller which will be executed before any other controller. I want in this controller to pass some variables to view. How is this possible?
Thank you.
You can do such thing in the before filter, in routes.php (, for Laravel 4 its filters.php).
Or if you need to do it in controller then create a BaseController which should extend Controller, in Laravel 3 you should have this already, base.php. And all your controllers should extend this BaseController, in its __construct or in before filter do this for ease:
View::share('key', 'value');
And use it as $key in your views.
Note, View::share can be problematic sometimes.
I made my first project with ZF, used the Zend_Tool component.
For the index controller a created some actions, and they work well.
But my question is, when do you create a new controller?
thanks!
As the Controller is a crucial part of the MVC pattern, I suggest reading up a bit on
this.
In a nutshell:
The controller receives input and initiates a response by making calls on model objects.
So when to create a new controller totally depends on your application. Often, there is one controller per database table in your model. The controller usually would allow for handling CRUD requests and delegating them to your model to process them.
Check out
http://framework.zend.com/manual/en/learning.quickstart.intro.html
http://akrabat.com/zend-framework-tutorial/
http://www.survivethedeepend.com/zendframeworkbook/en/1.0
When you want to have a different page. Are you using different logic or trying to fulfil a completely different task?
It should be quite clear to you when you want to create a new Controller, it is because you are doing something completely different.