Welcome Guest, Not a member yet? Register   Sign In
Defining extra constants CI 4
#1

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.
Reply
#2

(This post was last modified: 03-29-2020, 06:29 AM by jreklund.)

@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
Reply
#3

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.
Reply
#4

You can also use the Common.php file in the app folder.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 03-30-2020, 09:01 AM by dave friend.)

(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;
    }

Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB