D
D
Deamonik2017-07-04 21:47:01
Laravel
Deamonik, 2017-07-04 21:47:01

What PHP Write Styles Are There?

Hello!
I write on Laravel, I don’t like that it is constantly necessary to create 2 functions 1 for displaying the view 2 for the logic itself, for example, for processing the form, I currently do it in 1, I check the function if GET comes, then I give the view, if POST comes, I process the data, but I’m like this recording method is not very like if other recording styles? If yes, which ones and preferably with examples

public function register(Request $request)
 {
    if ($request->isMethod('GET')) {
         return view('frontend.user.registration');
         // тут отдаю вьюху view
     }

      if ($request->isMethod('POST')) {
         // тут обрабатываю сам запрос
      }
}

I think everyone understands what I'm doing. Any ideas for a different style of recording?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav, 2017-07-04
@stanislav-belichenko

I write in Laravel, I don’t like that it is constantly necessary to create 2 functions 1 for displaying the view 2 for the logic itself

In short, all your problems are due to the fact that you have not quite correctly misunderstood the MVC model. If you had built the structure of your application differently, then "two functions" would not have bothered you.
The MVC model, which is followed by almost all the frameworks that you can take up, assumes just such a structure - for each action there must be its own controller or its specific method, while the controller combines methods for any one logical unit.
Try to read exactly what about MVC, and most likely you will appreciate the beauty of this approach, since in the future it will allow you, with the growth of the application, to quickly navigate it, and not only you.
What you are currently using is not the right way at all. And it's also not the right way to have two "functions" in one controller (actually two methods of your controller class), one of which will be something like showRegister() and the other one will be createRegister(). The correct option in your particular example is to break your logic into two (three) logical units (two/three controllers or groups of controllers), one for displaying backend / frontend pages, and the other for processing authorization tasks. As a result, you should have something like:
app
...
├── Http
│   ├── Controllers
│   │   ├── Auth // 1. тут мы обрабатываем роуты, ответственные за авторизацию
│   │   │   ├── ForgotPasswordController.php
│   │   │   ├── LoginController.php
│   │   │   ├── RegisterController.php
│   │   │   └── ResetPasswordController.php
│   │   ├── Backend // 2. тут мы показываем бекенд
│   │   ├── Frontend // 3. тут мы показываем фронтенд
│   │   │   ...
│   │   ├── Controller.php
│   │       ...
...

In points 2 and 3, you use routes that are about authorization in the views they display, and you will end up with a separate group of routes like /auth/* and separate groups like /* (main) and /admin/*.
Agree, now everything looks logical and understandable. And "recording styles" have absolutely nothing to do with it.

A
Alexander Aksentiev, 2017-07-05
@Sanasol

What does it mean to create? Is there an inspector above you?
Write as you like, send at least all requests to the function, and then figure it out.
You yourself gave an example, why do you not like it?
Suits you, the rest is not important.

V
Vladimir, 2017-07-04
@djQuery

Read the docks for a couple of popular frameworks and choose what your soul is more in))))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question