CakePHP 3 : Define global contant variable

时光怂恿深爱的人放手 提交于 2019-11-30 09:52:26

问题


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 ?


回答1:


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 ?>" />


来源:https://stackoverflow.com/questions/37762119/cakephp-3-define-global-contant-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!