1

My url was like this: http://example.com/controller/method/param1/param2/param3/param4

My goal is: http://example.com/param1/param2/param3/param4

Here my controllers 'Home' and 'Read'. I also include my view and route configuration at bottom.

<?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

Please take a look at anchor below, that was my goal to produce 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>

When I defined the controller name, it's no problem. But I can't figure out how my url for single post only contain those four params

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

Below my route configuration:

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

Any help and advices will be appreciated.

Best Regards

2 Answers 2

2

Checking the docs you can place next route:

$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';

or if you are passing numbers/integers

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

You mustn't forget to pass arguments to your method as well, even as FALSE.

public function index($param1, $param2, $param3, $param4)
{
    //rest of code that is using params
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much @Tpojka, your solution works great. Now I'm struggling with 404 redirect occured when I remove/delete one of those params from url
Edited. Be aware of wildcard placeholder to be after known routes. If answer is valid, you can accept it to make it searchable from engines. Docs.
0

You can use custom routes in config/routes.php. For-Example

    #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

1 Comment

Do you mean $route['param1/param2/param3/param4'] is similar to $route['(:any)/(:num)/(:num)/(:any)'] according to my goal?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.