无法在codeigniter" slug"上加载特定页面。

时间:2018-04-28 21:40:33

标签: php codeigniter codeigniter-3

我试图在控制器中加载特定页面。我按照Codeigniter教程和主页工作,但单个页面(加载视图)不会根据给定的slug加载。

blog.php的

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

class Blog extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/index
 *  - or -
 *      http://example.com/index.php/Index/index
 *  - or -

 */

function __construct()
{
    parent::__construct();
    $this->load->model('blog_model');
    $this->load->helper('url_helper');
} 
public function index()
{
    $data['post'] = $this->blog_model->get_posts();
    $data['title'] = 'Blog archive';

    $this->load->view('header', $data);
    $this->load->view('blog', $data);
    $this->load->view('footer', $data);
}

public function view($slug = NULL)
{
    $data['post'] = $this->blog_model->get_posts($slug);    

     if (empty($data['post']))
    {
            show_404();
    }

    $data['title'] = $data['post']['title'];

    $this->load->view('header', $data);
    $this->load->view('post', $data);
    $this->load->view('footer', $data);
}
}

blog_model.php

<?php
class Blog_model extends CI_Model {

    public function __construct()
    {
            $this->load->database();
    }

    public function get_posts($slug = FALSE)
    {

    if ($slug === FALSE)
    {
            $this->db->select('*');
            $this->db->from('blog_posts');
            $this->db->join('category', 'category.id = blog_posts.category_id');
            $this->db->join('author', 'author.id = blog_posts.author_id');
            $query = $this->db->get();
            return $query->result_array();
    }

    $this->db->select('*');
    // $this->db->from('blog_posts');
    $this->db->join('category', 'category.id = blog_posts.category_id');
    $this->db->join('author', 'author.id = blog_posts.author_id');
    // $this->db->where('slug', $slug);
    $query = $this->db->get_where('blog_posts', array('slug' => $slug));
    return $query->row_array();
    }

}

正如您所看到的,我尝试了一些组合,因为我不确定当slug不是false时它会在get_posts中检索表。

2 个答案:

答案 0 :(得分:1)

试试这个

url_helper中调用__construct帮助,如下所示

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

现在,更新routes.php,如下所示

如果您的网址

  

http://www.example.com/blog/view/slug

你的ruote应该是这样的

$route['blog/view/(:any)'] = 'blog/view/$1';

如果您的网址

  

http://www.example.com/view/slug

你的ruote应该是这样的

$route['view/(:any)'] = 'blog/view/$1';

而且,您的get_posts模型函数重复查询,就像下面的

一样使用它
public function get_posts($slug = FALSE){
    $this->db->select('*');
    $this->db->from('blog_posts');
    $this->db->join('category', 'category.id = blog_posts.category_id');
    $this->db->join('author', 'author.id = blog_posts.author_id');
    if($slug){
        $this->db->where(compact('slug'));
    }
    $query = $this->db->get();
    return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
}

答案 1 :(得分:0)

最后,我通过修改路线解决了这个问题:

起初我有:

$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/$1';

所以我把它改成了:

$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/view/$1';

并将blog.php中的方法名称从blog更改为view。 &#34;视图&#34;看起来比#34; blog&#34;更标准。所以我就这样离开了。在&#39; blog / view / $ 1&#39;代表控制器,查看方法,当然$ 1是第一个参数。实际上,如果我尝试Blog / view / hello-world也可以。