Welcome Guest, Not a member yet? Register   Sign In
Configure URL for sake of SEO
#1

I have a site that using view_article/112 structure for get Article.
112 is article ID.  there is also a column at DB for slug.

So I can do something like this view_article/$slug
But I am not good with mvc.

So I want to view_article/the-article-title type structure.

Which source you can share with me to setup this.
I look for google and found only this
Reply
#2

(This post was last modified: 09-06-2019, 07:13 AM by PaulD.)

This is all relatively easy to achieve so you are unlikely to find any code samples to do this.

When you get your articles from the database ensure you also retrieve the article slug. When you are listing your article links in your view or menu view, make sure you construct your article links with your base_url/controller/method/slug format.

Your controller method will then look for the correct article based on the slug and retrieve the article details.

If you are conversant with retrieving data from the database, this should be no trouble at all. It is not really possible to post any example code as it is all too dependent on your site structure, db structure etc.

Best wishes,

Paul

EDIT: I take that back, slugs are used in the tutorial in the docs which should serve as a good starting point. https://codeigniter.com/user_guide/tutor...ction.html
Reply
#3

Hello there,

You must use your Routes file under Config. Please look for it. And let me give you an example :

1) $route['user-area'] = 'site/user_area';

the left of the  "="  is the url which the visitor will visit/see, the right part is your component. My component name is "site" here.

  Example :
 
PHP Code:
public function user_area(){
  $this->load->view('admin/user_area'); 


This example above points to your view file which is under your "admin" folder. But the url is not ('admin/user_area'). I mean, you want the visitor to see only "user-area". And this is arranged in the routes file.

2)$route['user-area/:num()'] = 'site/user_area/$1';
  
What is :num()  ? It means any number following user-area/. Because this page is for my "users personal page" depending on their ID. And ID is a number.

3) $route['user-area/:any()'] = 'site/user_area/$1'; 

 any means anything : text or number..

Plus, when you need to see a specific page of any product or user you will follow the url. So that you can get its/his slug.

Watch here: www. mysite.com/about-me    the slug is : about me. So, how to take it?
$the_slug = $this->uri->segment(1);

So then, when you go to Models to retrieve any data you will use this parameter:


Example for getting slug for Component and Model

Controller: 
 
PHP Code:
public function about (){
$the_slug = $this->uri->segment(1);
$this->load->model('Backend_Model);
$data['
informations'] = $this->Backend_Model->about_pages($the_slug); 
  
$this->load->view('
about-me', $data);  // in your about_me file you will use "informations" to  show the datas.



Model : 

 
PHP Code:
public function about_pages($the_slug) {
    $query $this->db->select('*')
     ->from('pages')
     ->where('page_slug'$the_slug)
    ->get()
    ->row();  

  
return $query;



Good Luck.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB