skip to Main Content

i’m trying to add dynamic routes from database in CI. but my website is really slow after adding data into my table.

anyway to performed routes.php file? or anyway to speed up my site?

NOTE: i have more than 60,000 record for SEO friendly url.

routes affected my response server time, its about 4 seconds.

here is my code:

require_once(BASEPATH . 'database/DB' . EXT);
require_once(BASEPATH . 'helpers/url_helper' . EXT);
require_once(BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();

$query = $db->query('select id,title from news');
$result = $query->result();
foreach ($result as $row) {
    $string = strtolower(str_replace(' ', '-', $row->title));
    $route["funny-news/" . $string] = "news/newsDetails/$row->id";
}

Thanks.


EDIT:

newsDetails Controller code:

public function newsDetails($id)
    {
        $hdata['active'] = "news";
        $result['news'] = $this->mytestdb->getNewsById($id);
        $this->load->view('nheader', $hdata);
        $this->load->view('newsDetails', $result);
        $this->load->view('footer');
    }

2

Answers


  1. My advice would be to build your own subclass(MY_Controller).In your controller load the database and write routing directly into routes file with file_put_contents.

    Login or Signup to reply.
  2. Open your routes.php and put the following line in

    $route['funny-news/(.*)'] = "news/newsDetails/$1";
    

    And your controller should look like

    public function newsDetails($slug)
    {
        $hdata['active'] = "news";
        $result['news'] = $this->mytestdb->getNewsBySlug($slug);
        $this->load->view('nheader', $hdata);
        $this->load->view('newsDetails', $result);
        $this->load->view('footer');
    }
    

    One thing – you should use an extra field in your db for your desired slug.

    A function can be found here PHP function to make slug (URL string).

    With this method you don’t need to fill the DB with unnecessary routes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search