How do I greate generic CURD for laravel resourceful controllers? - php

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.

Related

Laravel - Using Controllers

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!

Can we use helper in controller laravel?

I am new in laravel and I have good experience in Cakephp. In Cakephp we use helper for views only But I have seen my exiting code in laravel, We use helpers in controller also. Is it fine to use helper in controller or not?
If yes then Please let me know, What is the disadvantage or advantage of using helpers in controllers?
Please suggest me...
If you're talking about global Laravel helpers, then yes, you can use them everywhere in your app (controllers, models, view, middleware, routes.php etc), because these are global helpers.
You can also define your own helpers which will also be available globally.

How can I overwrite a core laravel method?

Here is what I'm trying to do:
We are using Laravel 4.2 in our project, and are using the frameworks' Password::remind functionality to send emails for password reset.
The problem is that the team wants all the email templates to be located inside the database instead of the views folder, so I will have to somehow pass a string to the Illuminate\Auth\Reminders\PasswordBroker::sendReminder method.
How can I override this class in Laravel so I can make this thing work? I'm currently a Laravel newbie so I don't yet fully understand how the framework works...
It isn't easy to override the classes simply. Whole laravel mail is based on views. But you can probably create a workaround to achieve you're goal.
For this you've to make the required views. In a service provider or in you're route file you make a view composer. With that view composer you retrieve your data from the database and the only thing you do in the view is printing the value.
View::composer(array('reminders.password','reminders.other'), function($view)
{
$view->with('html', RemindersRepository::getHtml());
});
Or something like that. Now in you're view print {{$html}} and it works!
Edit:
For you're information a view composer is something like a event listener. When the view is loaded, the callback function of the composer is loaded. In that callback function you can pass a extra variable with some contents. In the view you can print this value added in the composer.
Here is a basic guide on how to override/extend core functionality in Laravel:
You can create a folder in app/start/ and then create your own class to override the default behavior like NewReminderServiceProvider.php Then you extend the core functionality in question:
class NewReminderServiceProvider extends Illuminate\Auth\Reminders\ReminderServiceProvider {}
then overwrite or extend the registerPasswordBroker.
In the parent you are extending, you will see where it sets the view:
$view = $app['config']['auth.reminder.email'];
change that to be database driven however you want.
then last of all you have to swap out the ReminderServiceProvider with your NewReminderServiceProvider in your app/config/app.php and you are good to go. This will work with almost any core functionality. Replace or extend blade, auth, etc.

Best practice in CakePHP for call function in another controller

I have an controller attachment responsible for handling all uploads files. This controller uses a component to perform this control.
Now I have the following problem, I need call a function implemented in attachments controller from another controller, what is the best practice in this case? whereas:
It is not advised to call a function from a controller to another.
If I implement the function in my model this would have to use a component that is also not advised.
You don't. It's completely wrong and violating the MVC pattern.
Files represent a single entity in the system and should be handled as those (IMO) and in the model layer, not in the controller. You implement the validation and saving logic in a single model and access that model through associations from other places if you have too.
For example User hasOne Avatar, Gallery hasMany Image. Where the Avatar and Gallery association is your attachments or files table model.
I've written a plugin that does exactly the above and a little more
https://github.com/burzum/cakephp-file-storage
Use that or move your controller logic into a model. Controllers should be really really skinny and only have, well, controlling logic, they should never care about data handling or manipulation.

Laravel - controller executed before everything else

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.

Categories