Codeigniter-路由访问模型和数据库 [英] Codeigniter - Access Models And Database on Routing

查看:108
本文介绍了Codeigniter-路由访问模型和数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划在Codeigniter 3.0.3中创建一个项目,并且我想使用如下所示的路由.

I'm Planning to make a project in codeigniter 3.0.3 and I want to use routing like below.

1). www.mydomain.com/categoryName此处
2). www.mydomain.com/postName此处

1) . www.mydomain.com/categoryNamehere
2) . www.mydomain.com/postNameHere

我在数据库中有一个单独的表,用于保留类别名称及其唯一ID.
我想要的是当用户单击诸如www.mydomain.com/xxxxx
的链接时 1.首先检查类别表(xxxxx)
2.如果不匹配,则将其发送(xxxxx)到发布控制器.
如何在Codeigniter 3.0.3上实现此功能?
我试图在config/routing.php中访问我的模型,也试图直接在路由页面中执行mysql代码(活动记录).

I have a separate table in my database to keep category names with their unique id's.
What I want is when a user click on a link like www.mydomain.com/xxxxx
1.first check on category table (xxxxx)
2. if no match send it (xxxxx) to post controller.
How can I implement this on Codeigniter 3.0.3 ?
I tried to access my models in config / routing.php and also I tried to execute mysql codes (active records) directly in routing page.

推荐答案

要实现建议的url结构,我们必须创建一个中央调度程序

To implement the proposed url structure, we must create one central dispatcher that would

  1. 分析请求的URL.
  2. 将查询数据库以查找和显示类别.
  3. 如果未找到类别,它将尝试查找并显示文本.

听起来像控制器的工作.但是,我们如何使控制器响应每个请求呢?借助通配符路由!

Sounds like the job for a controller. But how do we make a controller that responds to every request? With the help of wildcard routing!

application/config/routes.php

$route['.*'] = 'default_controller';

现在,每个请求(无论URI如何)都将被路由到Default_controller.php.

Now every request, regardless of URI, will be routed to Default_controller.php.

但是我们如何在不知道将调用哪种方法的情况下编写控制器?有一种方法:内置的控制器服务方法_remap.

But how do we write controller without knowing what method will be called? There is a way: the built-in in controller service method _remap.

来自文档:

如果您的控制器包含一个名为_remap()的方法,则无论您的URI包含什么内容,它将始终被调用.

If your controller contains a method named _remap(), it will always get called regardless of what your URI contains.

所以我让自己幻想并为您创建一个概念Default_controller:

So I've let myself fantasize and create a concept Default_controller for you:

application/controllers/Default_controller.php

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

class Default_controller extends CI_Controller {

    // Pseudocode ensues 
    public function _remap()
    {
        // www.mydomain.com/(someTextHere)
        $slug = $this->uri->segment(1);

        $result = $this->load_data($slug);

        echo $result;
    }

    private function load_data($slug)
    {
        // Trying to find a category
        $category = $this->category_model->find($slug);
        if($category !== false)
        {
            // Presumably loads view into buffer
            // and returns it to the calling method
            return $this->load_category($category);
        }

        Trying to find post
        $post = $this->post_model->find($slug);
        if($post !== false)
        {
            return $this->load_post($post);
        }

        // Neither category nor post found
        show_404();
    }

    private function load_category($category)
    {
        // http://www.codeigniter.com/user_guide/general/views.html#returning-views-as-data
        return $this->load->view("category", array("category" => $category), true);
    }
}

注意:在最新下载的 Codeigniter 3.0.3 <中测试了此答案

这篇关于Codeigniter-路由访问模型和数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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