Jump to content

url rewriting and top menu editing


Krapoutchniek

Recommended Posts

Hi there,

 

I'm new on Prestashop and I've got two problems :

 

1) I am currently developping modules. One of them can create and manage news displayed on the home page. Each news can be viewed on its own page and uses this type of link :

 

/module/dashhomenews/newschampagne?id=1

 

I want to rewrite thoses links :

 

/news-champagne/1-this-is-a-news

 

where 1 is the id and this-is-a-news, the title of the news. For the moment I just want to rewrite the link so only the id appears. I've tried to override Dispatcher.php. It didn't work. Now I directly create my new route in the module's class. Here is the code :

<?php
	class DashHomeNews extends Module
	{
		public static $ModuleRoutes = array(
		    'news-route' => array(
		        'controller' =>  null,
		        'rule' =>        'news-champagne/{id[spam-filter]',
		        'keywords' => array(
		            'id'            =>   array('regexp' => '[0-9]+', 'param' => 'id')
		        ),
		        'params' => array(
		            'fc' => 'module'
		        )
		    ));
    
	    public function __construct()
	    {
	        $this->name 		= 'dashhomenews';
	        $this->tab 		= 'dashboard';
	        $this->version 		= 1.0;
	        $this->author 		= 'Yvan le Fevere de ten Hove';
	        $this->displayName 	= $this->l('News');
	        $this->description 	= $this->l('News on the home page.');
	 
	        parent :: __construct();
	 
	    }
	 
	    public function install()
	    {
	    	$tab = new Tab();
		    $tab->class_name = 'AdminNews';
		    $tab->id_parent = 12;			// 12 = ID du menu promotions
		    $tab->module = $this->name;
		    
		    $languages = Language::getLanguages();
		    
			foreach ($languages as $language)
			{
			   $tab->name[$language['id_lang']] = $this->l('News');
			}
			
			$tab->add();
		  
	    	$sql= "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."news`(
			    `id_news` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			    `date` DATETIME NOT NULL,
			    `active` BOOLEAN DEFAULT 1 NOT NULL)";
		   
		    if(!$result=Db::getInstance()->Execute($sql))
		    return false;
		    
		    $sql= "CREATE TABLE IF NOT EXISTS `"._DB_PREFIX_."news_lang`(
			    `id_news` INT(11) NOT NULL,
			    `id_lang` INT(11) NOT NULL,
			    `title` TEXT NOT NULL,
			    `summary` TEXT NOT NULL,
			    `name` TEXT NOT NULL)";
		   
		    if(!$result=Db::getInstance()->Execute($sql))
		    return false;
		    
	        return parent :: install()
		    && $this->registerHook('home') && $this->registerHook('ModuleRoutes');
	    }
	    
	    public function uninstall()
	    {
    		Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'news`');
    		Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'news_lang`');
	    	
	    	$tab = new Tab(Tab::getIdFromClassName('AdminNews'));

  		$tab->delete();
	    	
	    	parent::uninstall();
	    }
	 
	    public function hookDisplayHome($params)
	    {
	    	global $smarty;
	    	
	    	$this->context->controller->addCSS(_MODULE_DIR_.'/dashhomenews/css/news.css');
	    	
	    	$sql = new DbQuery();
			$sql->select('*');
			$sql->from('news', 'N');
			$sql->innerJoin('news_lang', 'NL', 'N.id_news = NL.id_news');
			$sql->where('N.active = 1 AND NL.id_lang = ' . (int)$this->context->language->id);
			$sql->orderby('N.date DESC');
			$sql->limit(3);
			
			$tab_news = Db::getInstance()->ExecuteS($sql);
			
			$smarty->assign('language', $this->context->language->id);
	    	$smarty->assign('tab_news', $tab_news);
	    	
	        return $this->display(__FILE__, 'home.tpl');
	    }
	    
		public function hookModuleRoutes()
		{
		    return self::$ModuleRoutes;
		}
	}

But it doesn't work neither. And even if it workd, I don't know how to create the link with $this->getPageLink() in tpl file :(

 

 

2) For another module (one that shows price ranges) I need to add a link on the top menu. This link has to act like categories : the main link (called "Prices") has "sub-links" (each one is a price range). As I don't want the admin to link himself a product and a price rang (this is automatically done, based on the product's price), thoses price ranges can be categories. This is something else. I created tables in myd DB for that. How can I add this link to the top menu?

 

Thank you.

 

(sorry for my english, I'm French speaker)

Edited by Krapoutchniek (see edit history)
Link to comment
Share on other sites

Ok so nobody wants to help...

 

I finally found the solution. I share it here. So other people won't need to wait for answers that will never come...

 

I just made a little mistake when defining my new route, but it was explained nowhere but here :

 

https://www.packtpub.com/books/content/using-front-controllers-create-new-page

 

(Great thanks to this guy)

 

Here was my route :

public static $ModuleRoutes = array(
		    'dashhomenews-newschampagne-route' => array(
		        'controller' =>  'newschampagne',
		        'rule' =>        'news-champagne/{id}',
		        'keywords' => array(
		            'id'            =>   array('regexp' => '[0-9]+', 'param' => 'id')
		        ),
		        'params' => array(
		            'fc' => 'module',
					'module' => 'dashhomenews'
		        )
		    ));

But in fact, you must name it "module-module_name-controller_name" and nothing else!

 

So the correct code is :

public static $ModuleRoutes = array(
		    'module-dashhomenews-newschampagne' => array(
		        'controller' =>  'newschampagne',
		        'rule' =>        'news-champagne/{id}',
		        'keywords' => array(
		            'id'            =>   array('regexp' => '[0-9]+', 'param' => 'id')
		        ),
		        'params' => array(
		            'fc' => 'module',
					'module' => 'dashhomenews'
		        )
		    ));

Maybe someone can help me for point 2?

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...