0

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');
    }
msDead
  • 35
  • 10

2 Answers2

0

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.

Sergiu Vintu
  • 47
  • 1
  • 5
  • Are you sure?I used this approach for a very big platform that had generated routes...It worked fast because writing to files is very fast,but it also depends how your querries are done.And also you shouldn't have to rewrite your routes on every request, – Sergiu Vintu Dec 06 '17 at 14:33
0

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.

Atural
  • 5,389
  • 5
  • 18
  • 35
  • but i need my specific , route string, for each id , was your code handle that? like `$route['funny-news/a']="news/newsDetails/321";` – msDead Dec 06 '17 at 14:24
  • why do you need an id here? – Atural Dec 06 '17 at 14:46
  • only for creating special url and fetch my data from db.and i look my sitemap too , for indexing. – msDead Dec 06 '17 at 14:53
  • your sitemap should contain urls like `/funny-news/i-am-the-first-funny-news/` or something like that there is no need for an id here - and if you need a possibility to call it with an id anyway - just make a new function.. – Atural Dec 06 '17 at 15:06
  • you mean , i fetch my info from db , with name? like `i-am-the-first-funny-news` – msDead Dec 06 '17 at 15:26
  • yep exactly - by creating a new column in your database called `slug` or something like that - and creating that slug from your title with help from the linked `slugify` function – Atural Dec 06 '17 at 15:27
  • let me try that , come back as soon as i can. – msDead Dec 06 '17 at 15:41
  • i face new problem, if i need route to root directory , like `$route['(.*)'] = "news/newsDetails/$1";` and another one `$route['(.*)'] = "product/proDetails/$1";` , how can i handle it? – msDead Dec 06 '17 at 15:59
  • that won't work because how should the program know where to look and what happens if both titles are existent in products and news - which of these two would you like ? ;) in situations like this - you should always have a prefix in urls; e.g. for your products `/product/your-product` and for your news `/funny-news/i-am-the-first-funny-news/` – Atural Dec 06 '17 at 16:18
  • your answer is acceptable, one more question. any way to make routes.php Parallel ? that can run while other part running? – msDead Dec 06 '17 at 17:08
  • what do you mean with that ? – Atural Dec 07 '17 at 08:12
  • routes are pre load , and must fully load , then CI system can start after routes load. i mean can we do routes.php like ajax call that don't effected other part , and make CI independence from routes.php. – msDead Dec 07 '17 at 08:18
  • nope it won't work - but i don't understand the purpose of this ;) – Atural Dec 07 '17 at 08:20