【问题标题】:CodeIgniter URL repetitionCodeIgniter URL 重复
【发布时间】:2016-10-24 15:50:21
【问题描述】:

刚启动CodeIgnitor,虽然是第一次使用MVC结构,但遇到了一个从未见过的问题……主要在“表单”部分,也有在数据库展示方面。我也使用 Xampp。
我有一个表单来创建要插入数据库的项目,但是每当我单击提交按钮时,url 部分就会出错。

我的基本 URL 是:localhost/CodeIgniter-3.1.1/(CodeIgniter-3.1.1 是包含每个 php 文件夹的目录)。

所以表单页面的 URL 是:localhost/CodeIgniter-3.1.1/index.php/news/create

当我提交时,它是:localhost/CodeIgniter-3.1.1/index.php/news/localhost/CodeIgniter-3.1.1/index.php/news/create

它只是在控制器(新闻)之后重复整个 URL。 我不认为它必须与 config.php,我的基本 URL 看起来不错,我只是不知道。

【问题讨论】:

  • 是的,我们需要一些代码。可能是<form> 标签。

标签: php codeigniter url


【解决方案1】:

制作你的基本网址http://localhost/Codeigniter-3.1.1/index.php/,然后在你的<form>标签中设置这样的网址<form method="post" action="<?= base_url('news/create') ?>">

【讨论】:

    【解决方案2】:

    /application/config/config.php中设置$config['base_url']这样

     $config['base_url'] = http://localhost/Codeigniter-3.1.1/
    

    在您看来,请执行以下任一操作来创建 <form> 标记

    <form method="post" action="<?= base_url('news/create'); ?>">
    

    如果您已加载“表单助手”(documented here),请在视图中使用此行

    <?php echo form_open('news/create'); ?> 
    

    【讨论】:

      【解决方案3】:

      由框架处理,如下:

      <h2><?php echo $title; ?></h2>
      
      <?php echo validation_errors(); ?>
      
      <?php echo form_open('news/create'); ?>
      
          <label for="title">Title</label>
          <input type="input" name="title" /><br />
      
          <label for="text">Text</label>
          <textarea name="text"></textarea><br />
      
          <input type="submit" name="submit" value="Create news item" />
      
      </form>

      另外,当我把一个链接放到一个视图时,问题就发生了,比如:

      &lt;a href="&lt;?php echo 'news/'.$news_item['slug']; ?&gt;"&gt;

      它没有构建正确的 URL,而是沿着条形复制自身。

      【讨论】:

      • URL 重复是因为你没有正确设置 `$config['base_url']`,因为所有答案都试图告诉你。
      • 是的,当我得到答案时,我正在写这篇文章,当时我没有看到它......但它似乎有效。谢谢大家:D
      猜你喜欢
      • 2021-11-07
      • 2014-07-30
      • 2015-01-27
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多