【问题标题】:How to create URL & Read on Codeigniter?如何在 Codeigniter 上创建 URL 和阅读?
【发布时间】:2015-08-02 06:53:57
【问题描述】:

如何在 Codeigniter 中创建动态 URL?

我关注了存储基本文章详细信息的表格

 1. ID
 2. Article Title
 3. Content
 4. Tags
 5. Status
 6. Create Time
 7. Update Time
 8. Author ID
 9. Status

现在我想知道如何创建动态 URL,其中包含 Page IDArticle Title

例如http://www.domain.com/1/hello-codeigniter

现在在示例 URL 上方,我正在生成带有 IDArticle Title 的 URL。

  1. 检索文章内容的ID(用户点击文章时,从ID获取内容)。
  2. 安全 URL 的文章标题。

但我不想在 URL 中显示 ID 并且在用户重定向到详细信息页面时仍然获取内容。

这是我的代码:

查看: home_page.php

<div id="body">
        <?php for($i=0; $i<count($blogPosts); $i++): ?>
            <div class="post-preview">
                <?php
                    $postID = $blogPosts[$i]->id; // Get post ID
                    $postTitle = $blogPosts[$i]->title; // Get post Title
                    $urlTitle = preg_replace('/\s+/', '-', $postTitle); // Replacing space with dash
                    echo anchor('content/'.$postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL
                    echo "<br />";
                ?>
            </div>
            <hr>
        <?php endfor; ?>
    </div>

控制器: home.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index() {

        $this->load->model("HomeModel"); // Load HomeModel
        $data['blogPosts'] = $this->HomeModel->blogPosts(); // Get blog posts data from blogPostsData function
        $this->load->view("home_page", $data);

    }

}

型号: HomeModel.php

class HomeModel extends CI_Model {

    public function blogPosts() {

        $this->db->select('*');
        $this->db->from('blog_posts');
        $query = $this->db->get();
        return $query->result();

    }

}

编码后当我将鼠标悬停在锚链接上时,我得到了这个 URL:

http://localhost/codeIgniter/index.php/content/1/My-First-Blog-Post!

如何在 URL 中隐藏 index.phpcontent1

还有一件事是,当我单击链接时,我收到了以下错误消息:

An Error Was Encountered 
The URI you submitted has disallowed characters.

任何帮助将不胜感激!!!

【问题讨论】:

  • 我从来没有使用过 CI,但我会大胆猜测 site_url()?

标签: php codeigniter codeigniter-2 codeigniter-routing


【解决方案1】:

您可以为此使用routes

要删除index.php,请使用.htaccess 和以下代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

config.php

$config['uri_protocol'] = 'AUTO';

要从 url 中删除内容 从锚点中删除内容 in view

echo anchor($postID.'/'.$urlTitle, $postTitle, array('title' => $postTitle)); // Get post URL

我不认为你应该从 url 中删除 1。如果您将其从网址中删除,那么您如何识别您的帖子。所以最好把它保存在 url 中。或者,如果您想删除,则将此 ID 附加在帖子标题的末尾,例如

http://localhost/codeIgniter/My-First-Blog-Post!-1

并分解帖子ID的最后一个元素

An Error Was Encountered 
The URI you submitted has disallowed characters.

您收到此错误是因为您在网址中添加了!。为了使您的应用程序安全 CI 防止来自 url 的特殊字符。所以最好不要在 url 中使用特殊字符,_- 除外。

在您的控制器上

class Content extends CI_Controller {
    function index($id, $name)
    {
        echo $name;// this contain book name 
        echo $id;// this contain book id

        //your code
    }
}

现在在您的路线中application/config/routes.php

$route['(:num)/(:any)']='content/index/$1/$2';

【讨论】:

  • 感谢您的支持,但您能否通过我的示例进行更多解释,因为我不明白如何执行此操作。我已经用我的示例更新了我的问题,如果需要,我还会在我的代码中给出任何建议。
  • 抱歉回复晚了。我已经用你的答案更新了我的代码,一切都很完美,但是有没有其他方法可以通过隐藏的方式传递 id,因为我不想在 URL 上显示。我在 WordPress 上有博客,我无法从 URL 中看到 id,它会返回页面内容。我想做和 WordPress 一样的事情。有什么想法吗?
  • 如果我只从 URL 传递标题并通过阅读标题获取页面内容怎么办。这是正确的方法吗?需要你的建议。
  • 在标题的基础上创建一个唯一的链接然后你可以通过标题来做
  • 如果我的回答对您有帮助,请标记为正确答案,以便其他用户也可以从中获得帮助。
【解决方案2】:

如果您将 slug(安全 URL 的文章标题)存储在数据库中,则根本不需要公开 id。不过,这将涉及向您的表中添加一列。

您可以使用url_title() 自动生成文章创建时的 slug,详情为here

【讨论】:

    【解决方案3】:
    <a href="<?php echo base_url()?>index.php/controller_name/method_name/<?php echo $item['name'] ?>/<?php echo $item['id'] ?>" alt="">click here to view</a>
    

    所以您的网址看起来像 www.example.com/index.php/controller_name/method/book_name/book id

    在控制器中

    function method_name($name, $id)
    {
        echo $name;// this contain book name 
        echo $id;// this contain book id
    
        //Simply you can use this to get all data
        $result = $this->Model_name->get_book_details($id);//in model $id hold the id of book
    }
    

    【讨论】:

    • 感谢您的支持,但请您用我的示例进行更多解释,因为我不明白如何执行此操作。我已经用我的示例更新了我的问题,如果需要,我还会在我的代码中给出任何建议。
    猜你喜欢
    • 2019-08-25
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多