CodeIgniter Forums
Defining extra constants CI 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Defining extra constants CI 4 (/showthread.php?tid=75914)



Defining extra constants CI 4 - ZoeF - 03-28-2020

Hey

I am rewriting some of my smaller projects to get the hang of CI 4. But some things I am now rewriting I want to be able to reuse aftwerward without to much of a hassle. So I am trying to not meddle to much with the files allready included within CI 4 folders. One of those things I want to do is create my own little config with my own constants in there. But for the love of god I am unable to get it working.

It might be something stupid but if anyone has any working example i would be gratefull. Or if you have a better way of going about it i am all ears.

Just note I am in no way a experienced programmer.


RE: Defining extra constants CI 4 - John_Betong - 03-28-2020

@ZoeF

There must be numerous ways to achieve the task of having your own constants.

The way I do it is to create a Super Class and for all your Classes to inherit the super Class:

Code:
<?php
// CI_VERSION 4.??
Class My_SuperClass extends CI_Controller
{
  define('CONST_001', 'CONST_001');
  define('CONST_002', 'CONST_002');
  define('CONST_003', 'CONST_003');
}// endclass

Class blog extends  {
  // all constants will be available here
}// endclass



RE: Defining extra constants CI 4 - ZoeF - 03-29-2020

Mmm yeah thats how I did it in the past. I do however believe it should not be in the controller but in a config. In that way all is a bit more structured aswell.


RE: Defining extra constants CI 4 - InsiteFX - 03-29-2020

You can also use the Common.php file in the app folder.


RE: Defining extra constants CI 4 - dave friend - 03-30-2020

(03-28-2020, 05:25 PM)ZoeF Wrote: Hey

I am rewriting some of my smaller projects to get the hang of CI 4. But some things I am now rewriting I want to be able to reuse aftwerward without to much of a hassle. So I am trying to not meddle to much with the files allready included within CI 4 folders. One of those things I want to do is create my own little config with my own constants in there. But for the love of god I am unable to get it working.

It might be something stupid but if anyone has any working example i would be gratefull. Or if you have a better way of going about it i am all ears.

Just note I am in no way a experienced programmer.

You can create a class in the Config namespace and use it just like any other class.

PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
MyStuff extends BaseConfig
{
    define("FOO",  "something");

    public $bar "Hello World.";



Then when you want to use "MyStuff" you create an instance of it and use the public properties like any other class.

PHP Code:
<?php namespace App\Controllers;

class 
Home extends BaseController
{
    public function 
index()
    {
       $myStuff = new Config\MyStuff();
       echo 
$myStuff->bar;
    }




RE: Defining extra constants CI 4 - kilishan - 03-30-2020

Or add it to app/Config/Constants.php.


RE: Defining extra constants CI 4 - ramonpuig - 04-27-2024

(03-30-2020, 11:21 AM)kilishan Wrote: Or add it to app/Config/Constants.php.

 I have a problem, i defined a constant in constants.php but the call to it in the controller does not find it.



RE: Defining extra constants CI 4 - luckmoshy - 04-27-2024

(04-27-2024, 09:44 AM)ramonpuig Wrote:
(03-30-2020, 11:21 AM)kilishan Wrote: Or add it to app/Config/Constants.php.

 I have a problem, i defined a constant in constants.php but the call to it in the controller does not find it.

put it like this
PHP Code:
defined('YOUR_CONSTANT') || define('YOUR_CONSTANT''youraim'); 

use in your controller YOUR_CONSTANT .....


RE: Defining extra constants CI 4 - InsiteFX - 04-27-2024

I just checked it and it works fine with CodeIgniter 4.5.1