Ad

Where Should I Declare The Configurations For Entire Application In October Cms

- 1 answer

I have to set some constant values to use anywhere of the application. How can I set this constant. I am trying as follows:

I set the STATUS constant at layouts/admin.htm

 function onStart()
{
    if(!Auth::getUser()){
        \Flash::error($this['theme_lang']['not_allowed']);
        return Redirect::intended('login');
    }

    $this['STATUS']  = [
        'PENDING' => 0,
        'PACKAGING' => 1,
        'PICKUP' => 2,
        'REPICKUP' => 3,
        'PICKUPDONE' => 4,
        'ASSIGNTODELEVER' => 5,
        'REASSIGNTODELEVER' => 6,
        'DELIVERED' => 7,
        'PAID' => 8,
        'RETURNTOHUB' => 9,
        'RETURNTOMERCHANT' => 10,
        'REFUSED' => 100,
    ];
}

This is not standard way I used. I can't use STATUS as GLOABL constant through out the application. Would you suggest me any smart way please?

Ad

Answer

For Ex. we have users table and we need to add related status information for it

so we can add it as static attribute in User model

Ex. model: plugins\rainlab\user\models\User.php

<?php namespace RainLab\User\Models;

class User extends UserBase
{
    use \October\Rain\Database\Traits\SoftDelete;

    /**
     * @var string The database table used by the model.
     */
    protected $table = 'users';
    

    // HERE ----->
    public static $Status = [
        'PENDING' => 0,
        'PACKAGING' => 1,
        'PICKUP' => 2,
        'REPICKUP' => 3,
        // ... add more
    ]

    // .... other code  
}

Now, How to use it

// any where in php code
echo User::$Status['PENDING']; // output -> 0


// may be you need to pass it in twig section 
function onStart()
{
    $this['STATUS'] = User::$Status;
}

In this way, you can keep proper information like

  1. this status is about users
  2. in the future if you need to modify anything you just need to update in the User model
  3. easy to reference and use

if any doubt please comment.

Ad
source: stackoverflow.com
Ad