• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove segment from url in multi language site

#1
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.
Reply

#2
What's your problem?

If you want to remove `article`, just remove it.
PHP Code:
<a href="<?php echo base_url('it/'.$a['slug']);?>">Blog Title</a
Reply

#3
(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)
Reply

#4
(02-20-2021, 04:19 AM)demyr Wrote: using only :any() should meet your needs. Or try (Confusedegment)

Thanks for reply and help me @demyr!

I tried to use only :any or Confusedegment:
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!
Reply

#5
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(); 
Reply

#6
(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!

Reply

#7
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.