Symfony like view helpers in laravel? - laravel

Symfony has this:
https://symfony.com/doc/4.1/components/templating.html#helpers
I can't find laravels equivalent? What do people use in Laravel in place of this?
I myself have tried creating custom helpers.php, which is good for some use cases I guess, but now I'm at the point, where I would like something a little bit more structured.
I am thinking maybe Facades would be be good option?
Is there anything else I could take a look at for Symfony's template helpers equivalent in Laravel?

I would say the closest thing to this is directives. It allows you to add new methods to blade that you can use.
Can find the docs here
You can also add a new facade if you need something more complex that will be accessable from the view layer as well.
Hope this helps!

Related

Laravel Create Function to be used in different Controllers/in the same Controller

It's more a general question, I want someone to point me to the direction I should go.
1) FUNCTION FOR SAME CONTROLLER: I have two methods: Store and Update in the same controller. They both should contain some complex request validation (which I can't do via validator or form request validation because it's too complex). This means for me now using the same code twice in two methods... How can I take this code, create a function and use it in both Store and Update methods just with a single line, like:
do_this_function($data);
2) FUNCTION FOR DIFF. CONTROLLERS: I have another code which I use in many different Contollers. It transliterates Russian letters into Latin ones ('Сергей' = 'Sergey' and so on). How and where should I register this function to be able using it in different Contollers?
transliterate_this($data);
I have read something about helpers. Should I use them? If you an experienced Laravel develper and say so, I will read everything about them. Or, if you advice something else - I'll read about that.:) I just don't want to spend time reading about something useless or "not right way to-do-it".
Appreciate any help!
1) You could create a form request validation or you could just create a private function that would handle the validation and return the result.
2) You can create a helpers.php file, see here, and you put your translation function inside. Then you can call it from anywhere:
In a controller transliterate_this($data);
In a view {{ transliterate_this($data); }}.
You can do complex validation even inside a FromRequest. You can override the getValidatorInstance for example, or any other method from the base class to plug your validation logic on top of the basic validation rules. Else just consider making an external class to handle that complex validation and inject it in your controllers using Laravel's IoC container.
You should create a facade to provide that feature. Then you can easily use it anywhere (and additionally create a helper method for it if that makes you feel better). Something like Transliterate::toLatin($str)
everyone! Thank you all for great answers. But I have discovered that the problem was that I didn't know anything about Object-Oriented Programming.
(Ans I was working in Laravel:)).
After I took an Object Oriented Bootcamp from Laracasts, I started 'seeing' how Laravel actually works and I know can easily create methods inside classes and use them in other classes.
https://laracasts.com/series/object-oriented-bootcamp-in-php
(of course, you can read something else on OOP, but Jeffrey Way has really outstanding explanation talent!)

Controllers in laravel

I would like to know when it's the best to create a controller.
I mean why can't we just use 1 controller for all our functions.
For example I have this controller called forumcontroller
why can't I just put all my functions in that controller and then do something like.
forumcontroller#function1
forumcontroller#function2
forumcontroller#function3
When is it best to create a new one?
It's a good practice to crate different controllers for different module like UserController for user module, ForumController for forum module, BookingController for booking module etc.. It's easy to handle your code..
It's all about readability, simplicity, refactoring and design patterns.

Which is better in Laravel - use Input::get() in Controller or Model?

I wanted to ask, looking from the 'best practices' side, which solution is better, getting a form data in Controller and passing it to Model, or retrieving it directly in the Model?
I use the first solution, which makes your controller methods very long and ugly, but I think it is still the right choice. But recently I have seen some other projects source code where the form data is retrieved in model, but it seems that it breaks the rule, where the model should not know, where the data comes from.
So which is the better practice?
As #lukasgeiter said in the Comments, I also say this would typically be done in the controller.
You may want to have a look at the corresponding laracasts on
MVC
Models
Controllers
Basically, you already said it. Models are just kind of "storage-interfaces" while Controllers are places, where the logic happens.
Also, have a look at MassAssigment, which cannot be done within the Model itsself but in the controller. This might be of interest in your case (without knowing the details).

Creating logs from controllers in CakePhp

What is the best way to create logs & history in CakePHP. I was thinking if there is a way to create a Helper "that accesses" one table and then I call this helper inside of each controller that I want. However, I have read that this is not MVC standard. How would it be the correct way to do that?
I appreciate your time spent answering me that. Thanks!
thats what behaviors are for.
I use the LoggableBehavior from here:
https://github.com/alkemann/CakePHP-Assets/blob/master/models/behaviors/logable.php
works like a charm. they are then fetched and displayed in the view, if you want to.
Just implement a different log engine for that purpose. The CakeLog class actually works somewhat like an observer and the log engines like listeners.
Simply use CakeLog::write() and your custom engine that could load your log model in the constructor using ClassRegistry::init() and pass the incoming data from write() to the model.
The manual will help you with that. http://book.cakephp.org/2.0/en/core-libraries/logging.html#creating-and-configuring-log-streams

Is it good idea to use i18n inside model files, symfony 1,4?

Hey there I was just wondering is it good to use i18n inside the model files.
Though I am suspecting only controller knows what language at the moment is active.
I just wanted to ask before moving on.
Sorry if it is not so complex question!
I think i18n should be use only in the View layer (not in the model). Calling up helpers for translation inside model to translate some text or date, seems to me more like a rendering problem rathen than a data manipulation one. So try to modifie your solution to fit the way symfony does this kind of stuff.
My advise is to try to keep the MVC stack as clean and abstract in each layer as possible. Once you go inside the rabbit hole, you wont be able to come back easy :P

Resources