I recently want to copy a website and prepare to use prestashop. I have never studied this in depth, but I am only modifying some external services.
Let's take a good look at things from today to facilitate subsequent development.
Index. php:
<? Php </p> <p> include (dirname (_ FILE __). '/config. inc. php '); // load some Configuration information </p> <p> if (intval (Configuration: get ('ps _ REWRITING_SETTINGS') = 1) // url rewriting <br/> $ rewrited_url = _ PS_BASE_URI __; </p> <p> // The page structure is clearly displayed, the header and tail are defined in a separate file. The uniform header and tail can change the content in the middle, <br/> // that is, the master page technology <br/> include (dirname (_ FILE __). '/header. php '); </p> <p> // load the module mounted to the mount point home. The variable HOOK_HOME allocated to the tpl file is a string, <br/> // saves the html structure displayed by the module. In the tpl file, {$ HOOK_HOME} You can display the Module <br/> // in the Module. in php class files, you can <br/> $ smarty-> assign ('hook _ home', Module: hookExec ('home ')); <br/> $ smarty-> display (_ PS_THEME_DIR _. 'Index. tpl '); </p> <p> include (dirname (_ FILE __). '/footer. php '); </p> <p >?>
Let's take a look at header. php:
<? Php </P> <p> // p3p sort ies (http://www.w3.org/TR/2002/REC-P3P-20020416/#compact_policies) <br/> header ('p3p: CP = "idc dsp cor Cura ADMA our ind PHY onl com sta" '); </P> <p> require_once (dirname (_ file __). '/init. PHP '); // initialize some Page Information </P> <p>/* CSS */<br/> define css_files1__theme_css_dir_.'global.css'] = 'all '; // define the CSS file and media information </P> <p>/* hooks are VOLONTARY out the initialize array (need those variables already assign Ed) */<br/> $ smarty-> assign (Array (<br/> 'hook _ header' => module: hookexec ('header '), <br/> 'hook _ left_column '=> module: hookexec ('leftcolumn'), <br/> 'hook _ top' => module :: hookexec ('top'), <br/> 'static _ token' => Tools: gettoken (false), <br/> 'Token' => Tools :: gettoken (), <br/> 'maid _, <br/> 'content _ only' => intval (tools :: getvalue ('content _ only') <br/> ); <Br/> // you can see the modules of the header, leftcolumn, and top mount points, all are loaded in this file </P> <p> If (isset ($ css_files) and! Empty ($ css_files) $ smarty-> assign ('css _ files', $ css_files); <br/> If (isset ($ js_files) and! Empty ($ js_files) $ smarty-> assign ('js _ FILES ', $ js_files); </P> <p> $ smarty-> display (_ ps_theme_dir _. 'header. TPL '); </P> <p >?> <Br/>
Then footer. php:
<? Php </p> <p> if (isset ($ smarty )) <br/>{< br/> $ smarty-> assign (array (<br/> 'hook _ RIGHT_COLUMN '=> Module: hookExec ('rightcolumn '), <br/> 'hook _ FOOTER '=> Module: hookExec ('footer'), <br/> 'content _ only' => intval (Tools :: getValue ('content _ only'); <br/> $ smarty-> display (_ PS_THEME_DIR _. 'footer. tpl '); <br/>}< br/> // This is relatively simple. In structure, it mainly loads the modules of the rightColumn and footer mount points. <br/>?>
From the above three files, we can clearly see the prestashop Page Structure:
The preceding three files are similar to: 'hook _ RIGHT_COLUMN '=> Module: hookExec ('rightcolumn ')
For such a module to load functions, it is necessary to look at the code in it.
The hookExec function in classes/Module. php:
/* <Br/> * execute modules for specified hook <br/> * @ Param string $ hook_name hook name <br/> * @ Param array $ hookargs Parameters for the functions <br/> * @ return string modules output <br/> */<br/> Public static function hookexec ($ hook_name, $ hookargs = array (), $ id_module = NULL) <br/>{< br/> If ((! Empty ($ id_module) and! Validate: isunsignedid ($ id_module) or! Validate: ishookname ($ hook_name) <br/> die (tools: displayerror (); </P> <p> global $ cart, $ cookie; <br/> $ Altern = 0; </P> <p> If (! Isset ($ hookargs ['cooker']) or! $ Hookargs ['cookies']) <br/> $ hookargs ['cookies'] = $ cookie; <br/> If (! Isset ($ hookargs ['cart']) or! $ Hookargs ['cart']) <br/> $ hookargs ['cart'] = $ cart; </P> <p> $ result = DB: getinstance () -> executes ('<br/> select H. 'id _ hook', M. 'name', hm. 'position' <br/> from ''. _ db_prefix _. 'module' m <br/> left join ''. _ db_prefix _. 'hook _ module' HM on HM. 'id _ module' = m. 'id _ module' <br/> left join ''. _ db_prefix _. 'hook 'H on HM. 'id _ hook' = H. 'id _ hook' <br/> where H. 'name' = /''. psql ($ hook_name ). '/' <br/> and M. 'active' = 1 <br/> '. ($ id_module? 'And M. 'id _ module' = '. intval ($ id_module ):''). '<br/> order by HM. 'position', M. 'name' DESC '); <br/> If (! $ Result) <br/> return false; <br/> $ output = ''; <br/> foreach ($ result as $ k => $ module) <br/>{< br/> $ moduleinstance = module: getinstancebyname ($ module ['name']); <br/> If (! $ Moduleinstance) <br/> continue; <br/> $ Exceptions = $ moduleinstance-> getexceptions (intval ($ module ['id _ hook ']), intval ($ moduleinstance-> ID); <br/> $ fileindex = basename ($ _ server ['php _ Self ']); <br/> $ show = true; <br/> If (! Empty ($ exceptions) and is_array ($ exceptions) <br/> foreach ($ exceptions as $ exception) <br/> if ($ fileindex = $ exception ['file _ name']) <br/> $ show = false; <br/> If (is_callable (Array ($ moduleinstance, 'hook '. $ hook_name) and $ show) <br/>{< br/> $ hookargs ['altern '] =++ $ Altern; <br/> $ output. = call_user_func (Array ($ moduleinstance, 'hook '. $ hook_name), $ hookargs); <br/>}< br/> return $ output; <br/>}
In this way, the module information on the mount point is obtained through the mount point name, and then these modules are traversed,
Generate an instance of the module, and then execute the corresponding hook + hook_name function in the module to return the corresponding html code,
The html code of all modules is recorded in the $ output variable as a string, so the returned result of the hook_Exec function is
A long constructed html string can be directly used by variables assigned to tpl.
The call_user_func () function is used to dynamically execute the methods in the module. This technique is good.