Sergej Fomin
Sergej Fomin

Reputation: 2002

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!

Upvotes: 0

Views: 629

Answers (3)

Sergej Fomin
Sergej Fomin

Reputation: 2002

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!)

Upvotes: 0

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

  1. 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.

  2. 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)

Upvotes: 1

tompec
tompec

Reputation: 1230

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); }}.

Upvotes: 1

Related Questions