【问题标题】:Hide codeigniter both controller and method name from url从 url 隐藏 codeigniter 控制器和方法名称
【发布时间】:2016-02-03 19:41:14
【问题描述】:

我的网址是这样的:http://example.com/controller/method/param1/param2/param3/param4

我的目标是:http://example.com/param1/param2/param3/param4

这里是我的控制器“Home”和“Read”。我还在底部包含了我的视图和路由配置。

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

class Home extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    /*
    *   Index method
    */
    public function index() {

        $this->db->select('a.*, c.*');
        $this->db->join('category as c', 'c.catid=a.catid');
        $this->db->limit(10);
        $this->db->order_by('a.id', 'DESC');

        $data['query'] = $this->db->get('article as a');

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

}


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

    class Read extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
        }

        /*
        *   Index method
        */
        public function index() {

            $id = isset($this->uri->segment(3)) ? $this->uri->segment(3) : FALSE;

            $this->db->select('a.*, c.*');
            $this->db->join('category as c', 'c.catid=a.catid');
            $this->db->where('p.id', $id);
            $this->db->limit(1);

            $data['query'] = $this->db->get('article as a');

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

    }

home_view.php

请看下面的锚,这是我制作http://example.com/param1/param2/param3/param4的目标

<div class="post-content">
    <ul>
        <?php foreach( $query->result() as $post ) : ?>
        <li><a href="<?php echo site_url($post->term_name.'/'.$post->term_id.'/'.$post->id.'/'.strtolower(url_title($post->title))); ?>"><?php echo $post->title ?></a></li>
        <?php endforeach; ?>
    </ul>
</div>

当我定义控制器名称时,没问题。但我无法弄清楚我的单个帖子的网址如何只包含这四个参数

<?php echo site_url('read/'.$post->term_name.'/'.$post->term_id.'/'.$post->id.'/'.strtolower(url_title($post->title))); ?>

在我的路线配置下:

$route['(:any)'] = "read/index/$1";

我们将不胜感激任何帮助和建议。

最好的问候

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    检查您可以放置​​下一条路线的文档:

    $route['(:any)/(:any)/(:any)/(:any)'] = 'controller/method/$1/$2/$3/$4';
    $route['(:any)/(:any)/(:any)'] = 'controller/method/$1/$2/$3';
    $route['(:any)/(:any)'] = 'controller/method/$1/$2';
    

    或者如果你传递数字/整数

    $route['(:num)/(:num)/(:num)/(:num)'] = 'controller/method/$1/$2/$3/$4';
    

    您也不能忘记将参数传递给您的方法,即使是 FALSE。

    public function index($param1, $param2, $param3, $param4)
    {
        //rest of code that is using params
    }
    

    【讨论】:

    • 非常感谢@Tpojka,您的解决方案效果很好。现在,当我从 url 中删除/删除其中一个参数时,我正在为 404 重定向而苦苦挣扎
    • 已编辑。请注意通配符占位符在已知路线之后。如果答案有效,您可以接受它以使其可从引擎中搜索。 Docs.
    【解决方案2】:

    您可以在config/routes.php 中使用自定义routes。举例

        #http://example.com/controller_name/method_name/param1/param2/param3/param4
    
            $route['param1/param2/param3/param4'] = 'controller_name/method_name';
    
           #Instead of -> 
    #http://example.com/controller_name/method_name/param1/param2/param3/param4
          # Display -> http://example.com/param1/param2/param3/param4
    

    【讨论】:

    • 根据我的目标,你的意思是$route['param1/param2/param3/param4']$route['(:any)/(:num)/(:num)/(:any)'] 相似吗?
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2017-06-25
    • 2016-05-23
    • 2012-11-01
    相关资源
    最近更新 更多