php - How to create seo friendly url with codeigniter? -


i leaning codeigniter few days , wondering create blog on codeigniter 2 question coming mind.

1. how create seo friendly url wordpress using.

2. how page content after navigate url.

i have created table tbl_post storing post details , table structure are:

 1. id  2. title  3. content  4. tags  5. status  6. create time  7. update time  8. author id  9. status 

now want create dynamic post url above table.

for example: http://www.example.com/hello-world/

and after navigate above url, how content of hello-world post?

you have notice have not passed id example url content. suggestion, if pass id , don't want show in url string?

that's it.

i thankful if guide me proper way.

thanks.


code review

home page (list view of blog posts)

home controller:

<?php if ( ! defined('basepath')) exit('no direct script access allowed');  class home extends ci_controller {      public function index()     {         $this->load->model("homemodel"); // load home model         $data['postdata'] = $this->homemodel->postdata(); // posts data postdata function          $this->load->view("global_header"); // include header area         $this->load->view("home", $data);         $this->load->view("global_footer"); // include footer area     }  } 

home model:

<?php if ( ! defined('basepath')) exit('no direct script access allowed');  class homemodel extends ci_model{      public function postdata()     {         $this->db->select('tbl_post.*, tbl_user.first_name, tbl_user.last_name');         $this->db->from('tbl_post');         $this->db->join('tbl_user', 'tbl_post.author_id = tbl_user.id', 'left join');         $query = $this->db->get();         return $query->result();         }  } 

home view

<?php for($i=0; $i<count($postdata); $i++): ?>         <?php              $postid =  $postdata[$i]->id;             $posttitle = $postdata[$i]->title;         ?>         <a href="<?php echo base_url("$postid/$posttitle"); ?>">             <h2 class="post-title">                 <?php echo $posttitle; ?>             </h2>         </a>     </div>     <hr> <?php endfor; ?> 

now url looks this: http://example.com/1/man-must-explore-and-this-is-exploration-at-its-greatest

domain: http://example.com id: 1 title: man-must-explore-and-this-is-exploration-at-its-greatest 

i have created view(post) display post content post id(id fetch url).

am going right way? need suggestion improve logic.

answer first question

codeignitor oop concept framework, use model view controller.(mvc architecture).

so each , every click on site goes controller. controller decide show or next.

if there 3 pages (ex: home, product, contact us) use 3 controllers(home=use default controller, product= use product, contact = use contact) it.

so url (if click product) shows www.test.com/product, if click contact shows www.test.com/contact.

answer second question

in table maintain auto-increment id always(must). can pass id controller , can data want.

for ex. load content page.(content on product).

your data showing page(view)

<?php  foreach ($product $variable) // $product data array send data { ?>    <div class="content">         <a href="[call controller here, in `product`]/[*call sub function*, use `show`]/ [*then pass here product id*]<?php echo $variable['id']"></a>     <div> <?php } ?> 

so url like(after click on product, assume id of product 25) www.test.com/product/show/25

then in product controller create

public function show($id)//$id variable assign value coming through url  {    1. code    2. load view } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -