Laravel: Will Helper functions work for it? - php

I am using Multiple code blocks of Mail:: and Gate::define() in my code. I rather want to make it as functions and call them. What approach would be useful? Should I create helper classes?

If You have some logic and you want to use this logic from many places, you must to encapsulate it, an in your situation it's not bad solution to create helper function or maybe helper class.
If You don't know how - here is very clear example to do it :
Creating a Helpers File

Related

Laravel - Use function in blade

I'm beginning to build an app using Laravel, and one of the challenges I'm now facing is that throughout my blade views, i need to show a bunch of different bootstrap modals. Because of this, i believe it would be a better approach to create some sort of helper method that takes a few parameters to define how the modal should look. But im currently not sure what is best practice of doing this. Am i even allowed to use such a helper method in a view?
How would you do this?
You can still process any data using the PHP tags (<?php echo 'hi'; ?>), or even using the complete namespace of the file and then call the desired function, like {{ \Carbon\Carbon::today() }}
You can create a helper file and then define methods which can be used globally, including the views. But in your case the ideal scenario would be to use a partial and then to include the partial with different parameters to change the properties. This would work exactly like a method, but cleaner and more aligned with views. This should help you, the sub-views are called partials.
https://laravel.com/docs/5.4/blade#including-sub-views

PHP MVC - Calling view functions from model, or controller?

After reading a fair few posts on Stack, and also some material recommended to me on line, the MVC pattern is quite open to interpretation, so please don't answer with another explanation. Thanks!
I'm reaching the end of designing my own PHP MVC now, and have two possible ways to move forward with the views.
But first, my controllers are currently quite dumb, not containing any classes and simply call public methods from the suitable models, and render the appropriate view.
Views are included as necessary, so naturally the objects are available to them via the constructors, along with all the methods (inc delete/update etc).
My first option is to get data using something like this in the view
foreach($object->fetchData() as $data)
and then looping through the results. However some people do not agree with this, and have suggested that such methods should be excluded from the view. Instead it has been recommended that I assign this to a variable in the constructor, and then use this variable in the view
//constructor
$fetched_data = $object->fetchData()
// view
foreach($featched_data as $data)
I don't like this very much, as it seams messy and unnecessary.
With all my critical methods being made available to the view, could this be considered a problem?
So here's the question. Do I keep it how it is, and create new methods in the MODEL for rendering data (as above) OR can I create a class in the constructor, extend the model into it, protect the critical functions in the model, and then create my read only public methods in the CONSTRCUTOR?
Thanks!
I would create a class in the constructor. Not only would extending the model be a much safer approach, but it'd also minimize the calling of functions in the views. Am assuming you'll have several views, it's much easier to get access the constructor data than calling method functions each time in each view.
You can add classes to your controllers that will call the method functions and pass the data directly into the views, instead of clustering your constructor or bootstrap.

Where to put big, rarely used functions in codeigniter

I have a several functions which are quite large, and are only used in one controller function each, and I'm wondering where to put these? They are not displaying any views, but instead crunching some numbers.
If I'm not wrong, there are 4 possible places where i could put these function: in my controller, in a helper, in a library or in a model. But none of these seem appropriate, since I don't want the code to be loaded every time a user uses the controller, and model should be used to do database stuff, and helpers and libraries should contain code that can be used over and over again.
If it is business logic, the best place to put it is in the controller as a private method, then you can call that method from within the controller.
Just as a note, helpers aren't always loaded unless you autoload them or load them in the constructor of your controller. So, as an alternative, you can make these methods of a helper then just load the helper in the controller action you wish to use them. That way they only get loaded when you need them.
CodeIgniter comes with helpers that you probably might not use (doesn't load unless you specify it in the application/config/config.php file) and I don't think its a problem having functions that you only use once stored there (application/helpers ). For example I might use a random password generator once only, but its still there and won't be loaded unless I call it.
$this->load->helper('my_string_generators');

Where implement query functions in CakePHP?

The CakePHP framework is a layered structure. My question is:
Where (theoretically) is the best place to implement data query functions? In models or controllers?
With "data query functions" I mean functions like getItemsWithSomeInfo($idItem) etc., i.e functions that are not actions.
Currently, I am implementing these functions in the controller, but I think there are other frameworks in which these functions (or methods) are implemented in the model layer.
I searched on Stackoverflow and found similar issues (Where/how to store custom functions (or methods) in CakePHP), but (in my opinion) are not good answers.
Its better to place such functions in your Model then in Controllers. All data related codes should be in the model. It is always a better idea in the long run, when it comes to refactoring or testing. You know exactly where to find the code. And it can be re-used without copy & paste in other actions/controllers.
Place you function in your corresponding model and then use it in your controller:
Example:
In Model.php
public function getItemsWithSomeInfo($idItem) {
// your code
}
In ModelsController.php
$this->Model->getItemsWithSomeInfo($id);

Is it okay to load models in Helpers in CodeIgniter?

I'm trying to use helpers to handle processes that involve multiple models. Is this okay in terms of best practices?
What sort of work is being done in the helpers? A library might be a better choice.
The codeigniter user guide describes helpers:
Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.
Think of them as akin to standard php functions. You can use them anywhere.
If it gives you the creeps to use helpers the way you are, consider your controllers as 'routers'. Use them to send information to a model and receive it back. Then you can send that info somewhere else, like another model. When you have all the info you need, you can use a helper or another function in the same controller to manipulate the information.
Personally, I mostly use helpers in my views to reformat or transform data.

Categories