Controllers in laravel - 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.

Related

Should all models be represented in DB, or better to make only a controller if modell is not represented in DB?

I wonder about the route leading to the welcome-page in my project. You reach it with the '/'-route .
Where should the method handling this route be and what is the correct structure behind it?
I get two ideas spontaneously:
Make a model named something like Home and put the method in HomeController
(Con: I thought all modells should be represented in DB?)
...
Make only a controller named HomeController, no model
(Con: Feels wrong to have a controller for a modell that does not exist?)
What is correct?
I can't comment so I'll leave it here.
Both Model and Controller are just Class. If you take a look at your HomeController and your User.php model you'll see they extend Controller and Model Classes, respectively.
As many said there is no need to have 1 to 1 relationship between Controllers and Models.
Invest some time to learn basics of OOP and you will see that you can have as much classes as you want, and there are many ways to relate them. Some of the Classes are Controllers, some are Models and some are something completely else, like Exceptions. But at all times keep in mind that, in the end, they are just Classes implementing some interface, using some traits, and extending other Classes.
It's worth to do this at the start of your learning process. It will steepen your learning curve a bit but in the long run it's well worth it.
You don't need a model for each controller or vice versa, just add them as needed according to your application: need CRUD actions or other interaction with your model. Try creating a WecolmeController as the HomeController is the default one for after login features (or change it accordingly if needed).

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

is it better to have different controller and different model in MVC

I'm new at the MVC model. Im still learning on how to use codeigniter framework.
So, i have some questions, is it better to have different controller and different model to perform some functions or is it better to combine all into one controller and one model?
Thanks.
It is all up to you. But consideration seems like having particular controller for one object like User or Article etc. Depending on controller you would like to have appropriate model for manipulating consisting data User_model.php or Article_m.php.
If you have some generic methods and code that you want to pull out from DB from many controllers, you might have something like Generic_m.php.
If you have some other functions related to some area you can make your own library and use those.
For simple functions that don'e belong to any specific set you can create your own helpers.

Which is better in codeigniter? Adding a function in a helper or adding a function in an extended base class

In a codeigniter project i have to do some set of stuff in one than one controller.
I code all that stuff in a function and now i need to call whenever necessary.
i think Writing this function in more than one controller is not good.
i have 2 options,
create a helper and write these function in that and include the helper in necessary controllers.
Since i have extended CI base controller (My_Controller) and most of my controllers are extended that controller, i can write this function to my base controller also.
I have confused which one is better, right way?
Which one will speed up the process?
Is the second way slows the site?
They are identical for all intents and purposes.
Using a helper allows you to make the code portable, so you can use it in other projects, or to be called from anywhere in the code base, in the case of a formatting function for example
If you were planning to put it in a controller, then MY_Controller is best bet
Just to help you on you endeavor what i do is: (this is just me)
If i need to use something in the views, i use a helper a custom one or built in.
If i want to do something on a controller that other controller will be using too and don't want it to mess up or crowd my controller i use a library (pretty much you can use a helper but i chose to use a library)
If i want to load lets say a method, to affect Globally or some of the controller i use the base controller. (you could also use helper or library)
The key is you are not restricted to one, choose the best that suits you, as the saying goes, there are many ways to skin a cat, but please don't skin a cat..

CodeIgniter Model Call to Model

Im using CodeIgniter 2.0.2 and I noticed while calling a Model from within a Model, you dont need to load it.
For instance, in a Controller you need to write
$this->load->model('my_model');
$this->my_model->my_function();
But in a Model it can load just like this
$this->my_model->my_function();
Should i avoid writing my code like this, or is this safe?
I would avoid writing my code like this, but for a different reason.
Models are generally loaded from controllers, so it seems strange that you would need one model to call another one. Are you sure that there is not a better way to structure your code, such as having a model base class or using a helper for common functionality?

Resources