codeigniter - APPPATH在控制器之后返回路径 [英] codeigniter - APPPATH returns path after controller

查看:91
本文介绍了codeigniter - APPPATH在控制器之后返回路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中排序路径有问题。

I've got problems with sorting out the paths in my app.

我使用APPPATH函数读取/写入我的数据库中的图像的字符串路径。问题是APPPATH将函数放在控制器之后

I'm using APPPATH function to read/write my images's string path in my db. The problem is that the APPPATH puts the function after the controller

这里是我的函数,用于获取css

here is my function for getting the css

<link rel="stylesheet" type="text/css" href="<?php echo '../' . APPPATH.'_data/css/style.css'; ?>" />

,当我转到 http:// localhost / project / index。 php / home it works great
css链接到 http://localhost/project/application/_data/css/style.css 这是正确的

and when i go to http://localhost/project/index.php/home it works great css links to http://localhost/project/application/_data/css/style.css which is correct

但我有这个其他控制器

http://localhost/project/index.php/member/index/1

css文件路径为

href="<?php echo '../../../' . APPPATH."_data/css/style.css"; ?>"

因为我知道我的链接到css到

because other wise my link to the css goes to

http://localhost/project/index.php/member/application/_data/css/style.css

我使用一个简单的模板结构,其中我有我的标题/内容/页脚在视图中的包含子文件夹,我发送数据到内容。很标准的东西。我只是不明白为什么APPPATH不工作,因为如果调用它给我的APPLICATION文件夹的PATH,而不管使用的类。

I'm using a simple template structure where i have my header/content/footer in an include sub-folder in views and I'm sending data to the content. Pretty standard stuff. I just don't get why the APPPATH does not work as if when called it gives me the PATH to the APPLICATION folder regardless the class that's been used.

推荐答案

APPPATH 实际上是系统文件,而不是前端资源。它是应用程序文件夹的路径,这是它应该使用的。 ../ relative / paths 很少在Codeigniter中工作,因为它们是相对于当前URL(而不是视图文件)。

APPPATH is really for system files, not frontend assets. It's the path to your application folder, and that's what it should be used for. ../relative/paths rarely work well in Codeigniter since they are relative from the current URL (not the view file). This is really a mess you're getting into.

简单的解决方案是改用 base_url()如果你想要更短的路径,这是我使用:

Simple solution is to use base_url() instead, but if you want shorter paths, this is what I use:

define('BASE_URI', str_replace('index.php', '', $_SERVER['SCRIPT_NAME']));

在大多数情况下,结果将是 / ,但这将考虑子目录中的CI安装。对于你,它应该返回 / project /

In most cases, the result will be /, but this will account for CI installations in sub directories. For you, it should return /project/.

不建议保留静态文件像CSS和图像混合到您的应用程序文件。大多数人创建一个名为 assets public 的目录来存储它们。

It's not recommended to keep static files like CSS and images mixed into your application files. Most people create a directory called assets or public to store them.

<link rel="stylesheet" href="<?php echo BASE_URI.'public/css/style.css'; ?>" />
<link rel="stylesheet" href="<?php echo base_url().'assets/css/style.css'; ?>" />

如果您真的需要将资产存储在应用程序文件夹中,那么只需删除所有相对路径。您不需要它们,因为 APPPATH 是绝对的:

If you really need to store assets in your application folder, then just drop all the relative paths. You don't need them since APPPATH is absolute:

<link rel="stylesheet" href="<?php echo APPPATH.'_data/css/style.css'; ?>" />

这应该可以在任何地方使用。

That should work from everywhere.

这篇关于codeigniter - APPPATH在控制器之后返回路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆