D
D
deepin2019-08-14 15:19:54
PHP
deepin, 2019-08-14 15:19:54

Is it possible to process a POST request like this?

This is the code

<?php


namespace app\controllers;


use app\models\Units;

class UnitsController extends AppController
{
    protected $unit;

    public function __construct($route)
    {
        parent::__construct($route);
        $this->model = new Units();
        $this->setSite('Техника');

        # Прописал этот код сюда
        if (isset($_POST['buy'])) {
            $this->unit = $this->model->getOneUnit($this->user->id, $_POST['buy']);
            $this->buyUnit();
        }
        if (isset($_POST['sell'])) {
            $this->unit = $this->model->getOneUnit($this->user->id, $_POST['sell']);
            $this->sellUnit();
        }

    }

    public function indexAction()
    {
        $site = $this->site;
        $user = $this->user;
        $data_units = $this->model->getAllUnits($this->user->id, 'ground');
        $next_unit = $this->model->getNextUnit($this->user->lvl, 'ground');
        $this->set(compact('site', 'user', 'data_units', 'next_unit'));

        # Чтобы сюда не прописывать

    }

    public function marineAction()
    {
        $site = $this->site;
        $user = $this->user;
        $data_units = $this->model->getAllUnits($this->user->id, 'marine');
        $next_unit = $this->model->getNextUnit($this->user->lvl, 'marine');
        $this->set(compact('site', 'user', 'data_units', 'next_unit'));

        # Чтобы сюда не прописывать

    }

    public function airAction()
    {
        $site = $this->site;
        $user = $this->user;
        $data_units = $this->model->getAllUnits($this->user->id, 'air');
        $next_unit = $this->model->getNextUnit($this->user->lvl, 'air');
        $this->set(compact('site', 'user', 'data_units', 'next_unit'));

        # Чтобы сюда не прописывать

    }
    
    # далее методы buyUnit() и sellUnit();
}


Is it possible in this way to transfer the processing of POST requests to the constructor?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MikUrrey, 2019-08-14
@deepin

It is possible, but not necessary.
It would be more correct to make a certain method (initUnit, for example), which would be added to each action.
Please note that the constructor will be executed BEFORE the action is called, and the label # In order not to prescribe here , you have it at the end of the action. Already at least at this point questions arise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question