We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Global variable in Controller Action

I am having a Controller names SalesController, which have two Actions : addSalesAction (which performs the form submit and save data to database) and another is uploadFileAction (which perform a file upload for the newly added sale entry).

Now I want to use the id of the newly added sale entry to the uploadFileAction. How do I set the Id as Global variable and how do I use it?

I am getting Id by this method :

            $this->db->begin();
            $db_sales_order = new SaleOrder();
            $cont = false;

            $db_sales_order->loadFromJson($data);
            if ($db_sales_order->save() === false) {
                $this->db->rollback();
                foreach ($db_sales_order->getMessages() as $message) {
                    $error_msg = $message . "\n";
                }
            } else {
                $cont = TRUE;
                $new_entry_id = $db_sales_order->id;
                }

$new_entry_id is the variable which store newly added sales entry id.

How to make it Gloabal so I can use it in another Action?

edited Oct '17

What you mean in another action? Like user makes new request? Then you need to return it to user and make request with this id, php doesn't know anything what happened in previous request. So any global variable won't be known in next request. It's how PHP works. If you want it in the same request then just pass it as an argument to method and don't make it an action imho, or use property in class or whatever or di.



77.7k
Accepted
answer
edited Oct '17

HTTP is stateless, so the two actions will never be able to see the same global variables. It's also a bad design pattern ;]

You could save the ID as a session variable in save, then check for that variable in upload.

function addSalesAction() {
    // logic
    $this->session->set('new_sale_id', $sale->getId());
}

function uploadFileAction() {
    if($this->session->has('new_sale_id')) {
        $saleId = $this->session->get('new_sale_id');
        $this->session->remove('new_sale_id');
        // logic
    }
    else {
        $this->flash->error('My error message');
    }
}

In addition to Lajos's answer.. Sometimes you may need a variable accessible in all actions for current controller. For example an inner menu. You want to use initialize() method of your controller, notice that if your ParentController has initialize method you should call it so you do not overwrite it (ofc depends what you want to do) :)

class PagesController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->view->innerMenu = ['edit', 'images'];
    }

    // Index
    public function indexAction()
    {

    }

    // Something else
    public function somethingAction()
    {

    }
}

Why just not upload file with whatever data you want? What's a problem with it? It's not intuitive to have one form to upload file and other with some data.

Why just not upload file with whatever data you want? What's a problem with it? It's not intuitive to have one form to upload file and other with some data.

I have to store the file location and the module and entry id for which the file has uploaded.