如何使用 Codeigniter 3 中 views 目录中的资产?
How can I use assets placed in the views directory in Codeigniter 3?
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 online newspaper/blogging application 4. 我有决定给它添加主题。该应用程序不是 HMVC,只有 MVC。
我认为对主题使用 Twig 模板引擎是个好主意。为此,我使用 CodeIgniter Simple and Secure Twig.
我想把每个主题对应的views和assets放在同一个目录中,并加上主题名称。
我正在尝试从视图目录加载主题的资产,例如:application/views/themes/mytheme/assets/css/main.css
.
在 post 控制器中,我添加了行 $this->twig->addGlobal('maincss', base_url('application/views/themes/mytheme/assets/css/main.css'))
;
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->twig->addGlobal('siteTitle', 'My Awesome Site');
//CSS, JS and other resources add to twig here, because PHP and Codeigniter functions are not available from Twig templates
$this->twig->addGlobal('maincss', base_url('application/views/themes/mytheme/assets/css/main.css'));
$this->twig->display('themes/mytheme/layout', $data);
}
在layout.twig我有:
<link rel="stylesheet" href="{{maincss}}">
我得到的不是预期的结果,而是 403 Forbidden 页面。为了解决这个问题,我将 application/views/themes
添加到 .htaccess:
RewriteEngine on
RewriteCond !^(index\.php|assets|application/views/themes/|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/ [L]
上述解决方案因我无法弄清楚的原因而失败。
我做错了什么?
创建一个文件夹并将其命名为 assets outside application 然后 link 使用 base_url 将其命名为:
<link rel="stylesheet" href="<?php echo base_url('assets/back/css/bootstrap.min.css"')?>">
根据 khawaja umar 的 建议,我将我的主题分为视图和资产:我在 myapp/themes/mytheme/assets
中有主题的资产(css、图像、脚本)以及 myapp/application/views/themes/mytheme
.
中的主题观点
在控制器中,我有这两行来处理团队的每个部分(资产和视图):
$this->twig->addGlobal('maincss', base_url('themes/mytheme/assets/css/main.css'));
$this->twig->display('themes/mytheme/layout', $data);
在 layout.twig
文件中我有:
<link rel="stylesheet" href="{{maincss}}">
我得出结论,这是最好的方法,包括从安全的角度来看。
我正在使用 CodeIgniter 3.1.8 和 Bootstrap 开发 online newspaper/blogging application 4. 我有决定给它添加主题。该应用程序不是 HMVC,只有 MVC。
我认为对主题使用 Twig 模板引擎是个好主意。为此,我使用 CodeIgniter Simple and Secure Twig.
我想把每个主题对应的views和assets放在同一个目录中,并加上主题名称。
我正在尝试从视图目录加载主题的资产,例如:application/views/themes/mytheme/assets/css/main.css
.
在 post 控制器中,我添加了行 $this->twig->addGlobal('maincss', base_url('application/views/themes/mytheme/assets/css/main.css'))
;
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
$this->twig->addGlobal('siteTitle', 'My Awesome Site');
//CSS, JS and other resources add to twig here, because PHP and Codeigniter functions are not available from Twig templates
$this->twig->addGlobal('maincss', base_url('application/views/themes/mytheme/assets/css/main.css'));
$this->twig->display('themes/mytheme/layout', $data);
}
在layout.twig我有:
<link rel="stylesheet" href="{{maincss}}">
我得到的不是预期的结果,而是 403 Forbidden 页面。为了解决这个问题,我将 application/views/themes
添加到 .htaccess:
RewriteEngine on
RewriteCond !^(index\.php|assets|application/views/themes/|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/ [L]
上述解决方案因我无法弄清楚的原因而失败。
我做错了什么?
创建一个文件夹并将其命名为 assets outside application 然后 link 使用 base_url 将其命名为:
<link rel="stylesheet" href="<?php echo base_url('assets/back/css/bootstrap.min.css"')?>">
根据 khawaja umar 的 建议,我将我的主题分为视图和资产:我在 myapp/themes/mytheme/assets
中有主题的资产(css、图像、脚本)以及 myapp/application/views/themes/mytheme
.
在控制器中,我有这两行来处理团队的每个部分(资产和视图):
$this->twig->addGlobal('maincss', base_url('themes/mytheme/assets/css/main.css'));
$this->twig->display('themes/mytheme/layout', $data);
在 layout.twig
文件中我有:
<link rel="stylesheet" href="{{maincss}}">
我得出结论,这是最好的方法,包括从安全的角度来看。