Carpe_diem Posted November 19, 2024 Share Posted November 19, 2024 Hi guys, I am currently working on a custom module and I am having a problem with the SEO friendly URLs. If I go directly to my page recepies mysite.com/si/recepti/sladice/jabolcni-strudel it works fine, but when I change the language with the prestashop default language switcher to lets say italian it displays the italian version of the recipe but my url is not mysite.com/it/recepti/desset/strudel-di-mele but it is mysite.com/it/module/recipes/recipe?category_rewrite=dessert&rewrite=strudel-di-mele Can someone please help me solve this. I am attaching my code: // Here is my routes method in main module file: public function hookModuleRoutes($params) { return [ 'recipes-recipe' => [ 'controller' => 'recipe', 'rule' => 'recepti/{category_rewrite}/{rewrite}', 'keywords' => [ 'category_rewrite' => [ 'regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'category_rewrite' ], 'rewrite' => [ 'regexp' => '[_a-zA-Z0-9-\pL]*', 'param' => 'rewrite' ], ], 'params' => [ 'fc' => 'module', 'module' => 'recipes', 'controller' => 'recipe', ], ] } // Here is my frontcontroller code: public $category_rewrite; public $rewrite; public function init() { parent::init(); $this->rewrite = Tools::getValue('rewrite'); $this->category_rewrite = Tools::getValue('category_rewrite'); } public function initContent() { parent::initContent(); $id_lang = $this->context->language->id; $data = Db::getInstance()->getRow(" SELECT n.*, nl.* FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe AND nl.id_lang = ".(int)$id_lang." WHERE nl.slug = '".pSQL($this->rewrite)."' AND n.active = 1" ); if (!$data) { $original_post = Db::getInstance()->getRow(" SELECT n.id_recipe FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.recipe WHERE nl.slug = '".pSQL($this->rewrite)."' AND n.active = 1" ); if ($original_post) { // Get the post in the current language $data = Db::getInstance()->getRow(" SELECT n.*, nl.*,nl.slug as post_slug, kl.slug as category_slug FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe INNER JOIN "._DB_PREFIX_."recipe_category_lang kl ON n.id_cat = kl.id_cat WHERE n.id_recipe = ".(int)$original_post['id_recipe']." AND nl.id_lang = ".(int)$id_lang." AND kl.id_lang = ".(int)$id_lang." AND n.active = 1" ); if ($data) { Tools::redirect($this->context->link->getModuleLink( 'recipes', 'recipe', ['category_rewrite' => $data['category_slug'], 'rewrite' => $data['post_slug'] ], true, $id_lang )); } } } $alternate_links = []; $languages = Language::getLanguages(true); foreach ($languages as $language) { $post_in_lang = Db::getInstance()->getRow(" SELECT nl.slug as post_slug, kl.slug as category_slug FROM "._DB_PREFIX_."recipe n INNER JOIN "._DB_PREFIX_."recipe_lang nl ON n.id_recipe = nl.id_recipe INNER JOIN "._DB_PREFIX_."recipe_category_lang kl ON kl.id_cat = n.id_cat WHERE nl.id_recipe = ".(int)$data['id_recipe']." AND nl.id_lang = ".(int)$language['id_lang']." AND kl.id_lang = ".(int)$language['id_lang'] ); if ($post_in_lang) { $alternate_links[$language['id_lang']] = [ 'href' => $this->context->link->getModuleLink( 'recipes', 'recipe', ['category_rewrite' => $post_in_lang['category_slug'], 'rewrite' => $post_in_lang['slug']], true, $language['id_lang'] ), 'iso_code' => $language['iso_code'], 'lang_id' => $language['id_lang'], 'name' => $language['name'] ]; } } $this->registerLanguageUrls($alternate_links); } protected function registerLanguageUrls($alternate_links) { $languages = Language::getLanguages(true); $links = []; foreach ($languages as $lang) { if (isset($alternate_links[$lang['id_lang']])) { $links[$lang['id_lang']] = $alternate_links[$lang['id_lang']]['href']; } } $this->context->link->lang_links = $links; } Link to comment Share on other sites More sharing options...
Prestashop Addict Posted November 19, 2024 Share Posted November 19, 2024 Dis you translate your front controller in SEO & url? Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 19, 2024 Author Share Posted November 19, 2024 16 minutes ago, Prestashop Addict said: Dis you translate your front controller in SEO & url? Do not understand what you mean if I translate front controller? If each language has its own controller? Then no, do I have to create a seperate controller for each language? Link to comment Share on other sites More sharing options...
Prestashop Addict Posted November 19, 2024 Share Posted November 19, 2024 If you have a front controller name myslug when you go to SE & url you can edit your front module controller dans change slug url for each language Link to comment Share on other sites More sharing options...
Carpe_diem Posted November 19, 2024 Author Share Posted November 19, 2024 5 minutes ago, Prestashop Addict said: If you have a front controller name myslug when you go to SE & url you can edit your front module controller dans change slug url for each language Ok, I understand for single page, but I have a lot of recipes not just the one I mentioned in my first post, I tried to add recept/{category_rewrite}/{rewrite} in SEO & URLs but I get an error, Link to comment Share on other sites More sharing options...
Kaylee91 Posted 10 hours ago Share Posted 10 hours ago I realize this is from a while ago, but I’ve seen similar issues when the language switcher pulls routes that aren’t rebuilt for custom modules. Have you tried flushing the SEO/URL settings and forcing PrestaShop to regenerate routes after adding your custom dispatcher rules? Also worth checking if your module’s front controller is marked as multilingual in the database. If anyone has found a cleaner fix, I’d love to hear it too Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now