0

can someone help me with this. I want to remove controller name from the url like this

www.site-name.com/controller_name/controller_functions/controller_arguments

to this

www.site-name.com/controller_arguments

for instance:

www.site-name.com/blog/display/blog-title

to this:

www.site-name.com/blog-title

Below is my controller class

 class Blog extends CI_Controller {

       public function index() {

         $data['blogs_data'] = $this->Blog_model->get_blogs();

         $data['main_view'] = "blog/blog_layout";

         $this->load->view('layouts/main', $data);
      }

       public function blog_display($page_url) {
        $data['blog_data'] = $this->Blog_model->get_blog($page_url);

        $data['main_view'] = "blog/blog_detail_layout";

        $this->load->view('layouts/main', $data);
       }
 }

as your can see from my controller the URL of my site to display blog going to be like this www.website-name.com/blog/blog_display/($page_url) <== whatever the $page_url going to be. Now, what I want is to be like this www.website-name.com/($page_url) <= so straight to the $page_url.

Thanks

Ace
  • 55
  • 6
  • 2
    Possible duplicate of [How to remove controller name from url making it clean in codeigniter](https://stackoverflow.com/questions/12863095/how-to-remove-controller-name-from-url-making-it-clean-in-codeigniter) – RangerRanger Oct 18 '18 at 02:28
  • Its not a duplicate – Ace Oct 18 '18 at 03:39

3 Answers3

0

Go to application->config->routes.php

Then you can set a route to "blog/display/blog-title" as "blog-title"

Add the below line to your routes.php file

$route['blog-title'] = 'blog/display/blog-title';

You can replace "blog/display/blog-title" to "blog-title" then.

shavindip
  • 607
  • 9
  • 27
0

define route in routes.php file.

route['controller_arguments']='controller_name/controller_functions';

on which onclick you are showing this url.define there this.

<a href="<?php echo base_url(); ?>controller_arguments">
                            </a>
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • so in route.php I can do like this $route['$page_url'] = 'blog/blog_display'; you know the $page_url is dynamic right? – Ace Oct 18 '18 at 05:26
0

I imagine that your slugs are generated so you can't just write all your slugs into your routes.

In you specific case you need something like this in your routes:

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

Do bare in mind that your routes are used from top to bottom. So if you have this route as your first one the rest of your site might not work.

So in case of a entire blog stucture you might want something like:

$route[''] = 'blog/index'; // For first page without pagination
$route['(:num)] = 'blog/index/$1'; // Blog article pagination (for second page and all other pages)
$route['(:any)] = 'blog/display/$1'; // Blog article detail

To avoid some problems in the future with having a route that is just a (:any) param, you might want to add an extra segment in that blog detail article.

Like so:

$route['detail/(:any)] = 'blog/display/$1'; // Blog article detail

For more information about this subject take a quick look at the docs here: Codeigniter routing system

marcogmonteiro
  • 2,061
  • 1
  • 17
  • 26