3

我对 Codeigniter URL 有疑问。我有一个控制器“welcome.php”:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $data['tiung'] = 'index';
        $this->load->view('welcome_message',$data);
    }

    public function dor($bus)
    {
        $data['tiung'] = $bus;
        $this->load->view('welcome_message',$data);
    }
}

和一个视图“welcome_message.php”:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
    sesuatu <?php echo $tiung?>
    <img src="fragor.jpg" width="720" height="246" alt=""/>
    ladalah
</body>
</html>

如果我想使用参数访问控制器功能“dor”,我使用了这个:

localhost/hostname/index.php/welcome/dor/something

它可以工作,但问题是图像未加载。我试图将图像文件放在 webroot 文件夹、“应用程序”文件夹,甚至“视图”文件夹中。但是图像仍然无法加载。我应该把图像文件放在哪里?

4

3 回答 3

4

最好的做法是在您的 webroot(带有 index.php 的文件夹)中创建一个 /images/ 目录,并使用该base_url()函数获取图像的链接,即 IE

<img src="<?php echo base_url('images/fragor.jpg'); ?>" width="720" height="246" alt=""/>

site_url()在使用or之前,不要忘记使用自动加载器或手动加载 url 帮助程序base_url()

此外,如果您使用 CodeIgniter 的重写规则从 URL 中删除 index.php(看起来不像您正在这样做),请不要忘记从重写中排除 /images/ 目录。

于 2012-08-24T13:22:41.950 回答
1

在 CodeIgniter 中有一种特定的方式来指示图像......比如

echo img('images/gautam.gif');

把它放在你的视图文件中,你需要在你的根目录下创建“images”文件夹

于 2012-08-27T07:33:50.463 回答
0

尽管您必须在自动加载配置中添加 CI 助手“html”,但这很容易。

<?php
$image_properties = array(
    'src'   => 'images/picture.jpg',
    'alt'   => 'Me, demonstrating how to eat 4 slices of pizza at one time',
    'class' => 'post_images',
    'width' => '200',
    'height'=> '200',
    'title' => 'That was quite a night',
    'rel'   => 'lightbox'
);
img($image_properties);
?>
/* <img src="site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a nigh`enter code here`t" rel="lightbox" />*/

或者很简单:

<?php
echo img('images/picture.jpg'); ?>
// gives <img src="site.com/images/picture.jpg" />
于 2017-10-20T02:17:19.653 回答