-
eleumas Member
  
-
Posts: 53
Threads: 24
Joined: Jun 2017
Reputation:
0
Hi! i have this code...
View:
PHP Code: <a href="<?php echo base_url('it/article/'.$a['slug']);?>">Blog Title</a>
Routes:
PHP Code: $routes->add('(:any)/it/article', 'It/Main::article/$1'); $routes->get('{locale}/article/(:any)', 'It/Main::article');
..and this link: www.mysite.com/it/article/title
I would like remove the word article form the link. My new link should be: www.mysite.com/it/slug
Thanks for the support.
-
demyr Senior Member
   
-
Posts: 274
Threads: 15
Joined: Sep 2018
Reputation:
6
02-20-2021, 04:19 AM
(This post was last modified: 02-20-2021, 04:19 AM by demyr.)
(02-19-2021, 12:00 PM)eleumas Wrote: Hi! i have this code...
View:
PHP Code: <a href="<?php echo base_url('it/article/'.$a['slug']);?>">Blog Title</a>
Routes:
PHP Code: $routes->add('(:any)/it/article', 'It/Main::article/$1'); $routes->get('{locale}/article/(:any)', 'It/Main::article');
..and this link: www.mysite.com/it/article/title
I would like remove the word article form the link. My new link should be: www.mysite.com/it/slug
Thanks for the support.
using only :any() should meet your needs. Or try (:segment)
-
eleumas Member
  
-
Posts: 53
Threads: 24
Joined: Jun 2017
Reputation:
0
(02-20-2021, 04:19 AM)demyr Wrote: using only :any() should meet your needs. Or try ( egment)
Thanks for reply and help me @demyr!
I tried to use only :any or  egment:
PHP Code: $routes->add('(:any)/it', 'It/Main::article/$1'); $routes->get('{locale}/(:any)', 'It/Main::article');
but i have this error:
Code: Trying to access array offset on value of type null
Maybe the issue is in my controller? This is my snippet:
PHP Code: public function article() { $slug = \Config\Services::request()->uri->getSegment(2); $data['article'] = $this->articlesModel->where('slug', $slug)->first(); $dataArticle = $data['article']; $data['title'] = $dataArticle['meta_title']; $data['metaDescription'] = $dataArticle['meta_description']; $data['images'] = $this->imagesGalleryModel->where('article_id', $dataArticle['id'])->findAll(); // latest articles $data['latestArticles'] = $this->articlesModel->orderBy('id', 'DESC')->where('status', 1)->findAll(5); // articoli più visti $builder = $this->db->table('tbl_articles'); $builder->orderBy('hits', 'DESC'); $data['hitsArticles'] = $builder->get(10); // article visit $builder = $this->db->table('tbl_articles'); $builder->set('hits', 'hits+1', FALSE); $builder->where('slug', $slug); $builder->update();
return view('article', $data); }
Thanks for the support!
-
demyr Senior Member
   
-
Posts: 274
Threads: 15
Joined: Sep 2018
Reputation:
6
It looks like you are trying to add and view the result within the same method (It/Main::article). Have you tried seperating them? Plus, can you see any response data from your controller, I mean for example:
PHP Code: $slug = \Config\Services::request()->uri->getSegment(2);
echo $slug; die();
-
eleumas Member
  
-
Posts: 53
Threads: 24
Joined: Jun 2017
Reputation:
0
02-22-2021, 08:04 AM
(This post was last modified: 02-22-2021, 08:32 AM by eleumas.)
(02-22-2021, 06:05 AM)demyr Wrote: It looks like you are trying to add and view the result within the same method (It/Main::article). Have you tried seperating them? Plus, can you see any response data from your controller, I mean for example:
PHP Code: $slug = \Config\Services::request()->uri->getSegment(2);
echo $slug; die();
Hi @demyr!
I have a view called blog with categories and articles for each category.
blog method:
PHP Code: public function blog() { $data['title'] = ''; $data['metaDescription'] = ''; $data['articles'] = $this->articlesModel->orderBy('id', 'DESC')->where('status', 1)->findAll(); $data['categories'] = $this->categoriesModel->where('lang', 'it')->findAll();
echo view('blog', $data); }
article method:
PHP Code: public function article() { $slug = \Config\Services::request()->uri->getSegment(2); $data['article'] = $this->articlesModel->where('slug', $slug)->first(); $dataArticle = $data['article']; $data['title'] = $dataArticle['meta_title']; $data['metaDescription'] = $dataArticle['meta_description']; $data['images'] = $this->imagesGalleryModel->where('article_id', $dataArticle['id'])->findAll(); // // // $data['latestArticles'] = $this->articlesModel->orderBy('id', 'DESC')->where('status', 1)->findAll(5); // // // $builder = $this->db->table('tbl_articles'); $builder->orderBy('hits', 'DESC'); $data['hitsArticles'] = $builder->get(10); // // // $builder = $this->db->table('tbl_articles'); $builder->set('hits', 'hits+1', FALSE); $builder->where('slug', $slug); $builder->update();
return view('article', $data); }
Routes:
PHP Code: // TEST $routes->add('(:any)/it/', 'It/Main::article/$1'); $routes->get('{locale}/(:segment)', 'It/Main::article/(:segment)');
// GOOD WITH WORD ARTICLE IN LINK: mysite.com/it/article/slug // $routes->add('(:any)/it/article', 'It/Main::article/$1'); // $routes->get('{locale}/article/(:any)', 'It/Main::article');
$routes->get('{locale}/blog', 'It/Main::blog');
The error is the same: Trying to access array offset on value of type null
https://demo.samuelesantoni.com/host/CI4-1-1.png
I have this code in another site with one language, CI 4.0.4 and PHP 7.4 and all works fine!
I tried your code and the uri segment is correct i can see the answer from my controller.
https://demo.samuelesantoni.com/host/CI4-result.png
Thanks for help me!
-
demyr Senior Member
   
-
Posts: 274
Threads: 15
Joined: Sep 2018
Reputation:
6
02-23-2021, 11:57 AM
(This post was last modified: 02-23-2021, 10:24 PM by demyr.)
Oh. Have you tried
PHP Code: $routes->get('{locale}/(:any)', 'It/Main::article/$1');
By the way, you said that you could retrieve data when you try on Controller but you didn't share it. And it seems like it cannot reach that meta_title.
|