


Use native PHP to write a routing function like CodeIgniter_PHP Tutorial
I wrote an api for a mobile application some time ago. I always used the query_string address, and all actions were distinguished based on an act parameter. This made it more difficult for developers to see. I originally wanted to rewrite it as "?c=controller&m=method&type=3&id=1", using the m parameter to load the file and instantiate it. Later, I saw that the sina weibo api routed the address. Also decided to follow suit on address routing. Originally, the CI framework had its own routing effect, but because I was considering writing an API, I wanted to write it more purely.
Support default controller (index) and method (index):
index.php index.php/controller index.php/controller/method index.php/controller/method/prarme1/value1 index.php/controller/method/param1/value1/param2/value2.....
<?php define('MODULE_DIR', './classes/'); $APP_PATH= str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__); $SE_STRING=str_replace($APP_PATH, '', $_SERVER['REQUEST_URI']); //计算出index.php后面的字段 index.php/controller/methon/id/3 $SE_STRING=trim($SE_STRING,'/'); //echo $SE_STRING.'<br>'; //这里需要对$SE_STRING进行过滤处理。 $ary_url=array( 'controller'=>'index', 'method'=>'index', 'pramers'=>array() ); //var_dump($ary_url); $ary_se=explode('/', $SE_STRING); $se_count=count($ary_se); //路由控制 if($se_count==1 and $ary_se[0]!='' ){ $ary_url['controller']=$ary_se[0]; }else if($se_count>1){//计算后面的参数,key-value $ary_url['controller']=$ary_se[0]; $ary_url['method']=$ary_se[1]; if($se_count>2 and $se_count%2!=0){ //没有形成key-value形式 die('参数错误'); }else{ for($i=2;$i < $se_count;$i=$i+2){ $ary_kv_hash=array(strtolower($ary_se[$i])=>$ary_se[$i+1]); $ary_url[pramers]=array_merge($ary_url[pramers],$ary_kv_hash); } } } $module_name=$ary_url['controller']; $module_file=MODULE_DIR.$module_name.'.class.php'; //echo $module_file; $method_name=$ary_url['method']; if(file_exists($module_file)){ include($module_file); $obj_module=new $module_name(); //实例化模块m if(!method_exists($obj_module, $method_name)){ die('方法不存在'); }else{ if(is_callable(array($obj_module, $method_name))){ //该方法是否能被调用 //var_dump($ary_url[pramers]); $get_return=$obj_module->$method_name($ary_url[pramers]); //执行a方法,并把key-value参数的数组传过去 if(!is_null($get_return)){ //返回值不为空 var_dump($get_return); } }else{ die('该方法不能被调用'); } } } else { die('模块文件不存在'); } ?>

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools
