Anuj TBE
Anuj TBE

Reputation: 9800

CakePHP 3 : Define global contant variable

I am working on CakePHP 3 project which is a little big.

I want to keep my application as much clean as possible by separating all media files from core application and that's why I have to store all media files on a separate subdomain as media.myproject.com and the project is accessible from www.myproject.com.

Also in media.myproject.com there could be many directories as

/root
|- users
   |- avatar
   |- cover
|- services
   |- logo
   |- banner
   |- slides
|- clients
   |- logo
   |- avatar
|- etc
   |- etc
   |- etc
   |- etc

Now, to be able to access files in application view I want to set global variables that I can use in any view like

<img src="<?= $media.$mediaUser.$userAvatar.$user->avatar ?>" />

How could I do this ?

Upvotes: 0

Views: 1786

Answers (1)

Jacek B Budzynski
Jacek B Budzynski

Reputation: 1413

You can make some like this:

config/Bootstrap.php

Configure::write('Media', array(
        'users' => array(
            'avatar' => 'media.myproject.com/users/avatar/',
            'cover'  => 'media.myproject.com/users/cover/'  
        ),
        'services' => array(
            'logo' => 'media.myproject.com/services/logo/',
            'banner'  => 'media.myproject.com/services/banner/' 
        )
 ));

YourView.ctp

<?php use Cake\Core\Configure; ?> 

<img src="<?= Configure::read('Media.users.avatar').$user->avatar ?>" />

Upvotes: 1

Related Questions