【问题标题】:Codeigniter url set to another pageCodeigniter url 设置到另一个页面
【发布时间】:2015-04-03 20:23:18
【问题描述】:

我正在使用 codeigniter,但我不知道如何将链接放入另一个页面。我的控制器文件名是 aboutus.php。我给了一个像

这样的链接
<a href="<?php echo base_url('aboutus'); ?>">AboutUs</a>

我的基本网址是

$config['base_url'] = "http://localhost/project/";
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

但上面的网址不起作用。我直接在浏览器中写了一个网址然后点击 http://localhost/project/index.php/aboutus 然后它工作正常。如何给一个网址?我很困惑。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    base_url() 会回显:

    http://localhost/project
    

    site_url() 将在哪里回显:

    http://localhost/project/index.php
    

    您想访问http://localhost/project/index.php/aboutus,但使用base_url() 您只能访问http://localhost/project/aboutus,这会给您带来错误。

    你可以做两件事,

    这个:

    <a href="<?php echo base_url('index.php/aboutus'); ?>">AboutUs</a>
    

    这意味着在aboutus之前添加index.php

    或者这个:

    <a href="<?php echo site_url('aboutus'); ?>">AboutUs</a>
    

    这意味着将base_url() 更改为site_url()

    确保您正在控制器中加载帮助程序:

    $this->load->helper(url);
    

    或在application/config/autoload.php 中转到以下行:

    $autoload['helper'] = array();
    

    然后这样做:

    $autoload['helper'] = array('url');
    

    它将包含在您现在拥有的每个控制器中。

    如果你启用了短标签,你可以这样写你的a标签:

    <a href="<?=site_url('aboutus');?>">About Us</a>
    

    或者如果你有 url 助手,你可以这样写:

    echo anchor('aboutus', 'About Us', 'title="About Us"');
    

    【讨论】:

    【解决方案2】:

    尝试用 site_url() 代替 base_url() 这样 index.php 就不会被跳过

    <a href="<?php echo site_url('aboutus'); ?>">AboutUs</a>
    

    https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

    此外,请确保已加载 url 助手。

    $this->load->helper('url'); //can also be done in config/autoload.php
    

    【讨论】:

      【解决方案3】:

      在使用 CI 函数 base_url() 和 site_url() 之前 您需要在 autoload.php 或控制器本身中加载 URL helper

      【讨论】:

        【解决方案4】:

        如果您在 PHP 中启用了短标签,您还可以编写链接:

        <a href="<?= site_url('controller_name/function_name') ?>">Link Text</a>
        

        【讨论】:

          【解决方案5】:

          在你的基础文件夹中使用这个 .htaccess

          RewriteEngine on
          RewriteCond $1 !^(index.php|resources|robots.txt)
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php/$1 [L,QSA]
          

          如果您仍然没有得到结果请在您的 apache 服务器中启用 mod_rewrite

          【讨论】:

            【解决方案6】:

            您的项目 URL 看起来好像您没有启用 php 短标签。那不是问题。试试这个代码:

            <a href="<?= site_url('index.php/project/aboutus') ?>">About Us</a>
            

            【讨论】:

              猜你喜欢
              • 2023-02-02
              • 2015-08-14
              • 2015-11-01
              • 2015-12-12
              • 2015-02-01
              • 1970-01-01
              • 2021-04-06
              • 2016-12-23
              • 1970-01-01
              相关资源
              最近更新 更多