Welcome Guest, Not a member yet? Register   Sign In
Remove segment from url in multi language site
#6

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

Reply


Messages In This Thread
RE: Remove segment from url in multi language site - by eleumas - 02-22-2021, 08:04 AM



Theme © iAndrew 2016 - Forum software by © MyBB