CakePHP 3:定义全局约束变量 [英] CakePHP 3 : Define global contant variable
问题描述
我正在使用一个有点大的CakePHP 3项目。
I am working on CakePHP 3 project which is a little big.
我希望通过将所有媒体文件与核心应用程序分开来保持我的应用程序尽可能干净这就是为什么我必须存储所有媒体文件在一个单独的子域为 media.myproject.com
,该项目可从 www.myproject.com访问 c。
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
.
在 media.myproject.com
$ c>目录为
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
现在,为了能够访问应用程序中的文件 view
我想设置我可以使用的全局变量任何查看
如
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 ?>" />
我该如何做?
推荐答案
您可以这样做:
config / Bootstrap.php
$ b
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 ?>" />
这篇关于CakePHP 3:定义全局约束变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!