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

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

the problem with that method is that the blog title is dynamic, I have more than 1 blog.
$route[''] = 'blog/display'; Im not sure about this but check it like this.
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>

1 Comment

so in route.php I can do like this $route['$page_url'] = 'blog/blog_display'; you know the $page_url is dynamic right?
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

Comments

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.