laravelн¨Ä£¿éÁ÷³Ì
laravelÊÇÒ»¿îÏÖ´ú»¯µÄphp¿ò¼Ü£¬ÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¹¤¾ß£¬Ê¹µÃ¿ª·¢Õß¿ÉÒÔ¸ßЧµØ¿ª·¢webÓ¦ÓóÌÐò¡£ÆäÖУ¬Ä£¿é»¯±à³ÌÊÇÒ»ÖÖ¼«ÎªÖ÷ÒªµÄ±à³ÌÍ·ÄÔ£¬ÔÚlaravelÖÐÒ²ÓÐןܺõÄÌåÏÖ¡£±¾ÆªÎÄÕ½«ÏÈÈÝÔÚlaravelÖÐн¨Ä£¿éµÄÁ÷³ÌºÍ°ì·¨¡£
Ò»¡¢½¨ÉèÄ£¿éĿ¼½á¹¹
ÔÚLaravelÖУ¬Ä£¿éÊÇÓÉһЩÏà¹ØµÄ¿ØÖÆÆ÷¡¢Ä£×Ó¡¢ÊÓͼ¡¢ÓïÑÔ°üµÈ×é³ÉµÄÜöÝÍ£¬Òò´Ë£¬ÔÚн¨Ä£¿éÇ°£¬ÐèÒªÏȽ¨ÉèÄ£¿éµÄĿ¼½á¹¹¡£ÔÚLaravelµÄ¸ùĿ¼Ï£¬½¨ÉèÃûΪ”Modules”µÄĿ¼£¬²¢ÔÚÕâ¸öĿ¼Ï½¨Éè”Example”Ä£¿éµÄĿ¼£¬½á¹¹ÈçÏ£º
app/Modules/Example ©À©¤©¤ Config ©¦ ©¸©¤©¤ config.php ©À©¤©¤ Controllers ©¦ ©¸©¤©¤ ExampleController.php ©À©¤©¤ Models ©¦ ©À©¤©¤ Example.php ©¦ ©¸©¤©¤ ExampleRepository.php ©À©¤©¤ Resources ©¦ ©À©¤©¤ Lang ©¦ ©¦ ©À©¤©¤ en ©¦ ©¦ ©¦ ©¸©¤©¤ example.php ©¦ ©¦ ©¸©¤©¤ zh ©¦ ©¦ ©¸©¤©¤ example.php ©¦ ©À©¤©¤ Views ©¦ ©¦ ©¸©¤©¤ index.blade.php ©¦ ©¸©¤©¤ assets ©À©¤©¤ Routes ©¦ ©À©¤©¤ api.php ©¦ ©¸©¤©¤ web.php ©¸©¤©¤ Providers ©¸©¤©¤ ExampleServiceProvider.php
µÇ¼ºó¸´ÖÆ
ÉÏÊöĿ¼½á¹¹ÖУ¬ConfigÄ¿ÈÎÃüÀ´´æ·ÅÄ£¿éµÄÉèÖÃÎļþ£¬ControllersĿ¼´æ·Å¿ØÖÆÆ÷Îļþ£¬ModelsĿ¼´æ·ÅÄ£×ÓÎļþ£¬ResourcesĿ¼´æ·ÅÆäËû×ÊÔ´Îļþ£¬ÈçÊÓͼ¡¢ÓïÑÔ°ü¡¢¾²Ì¬ÎļþµÈ£¬RoutesÄ¿ÈÎÃüÀ´´æ·Å¸ÃÄ£¿éµÄ·ÓÉÎļþ£¬ProvidersÄ¿ÈÎÃüÀ´´æ·Å¸ÃÄ£¿éµÄЧÀÍÌṩÕß¡£ÆäÖУ¬ExampleΪ¸ÃÄ£¿éµÄÃû³Æ£¬¿ÉÒÔƾ֤ÏÖʵÐèÇóÃüÃû¡£
¶þ¡¢ÉèÖ÷ÓÉÎļþ
ÔÚLaravelÖУ¬Â·ÓÉÊǽ«URIÓ³Éäµ½Ìض¨µÄ²Ù×÷µÄµØ·½¡£Òò´Ë£¬ÔÚн¨Ä£¿éʱ£¬ÐèÒªÉèÖøÃÄ£¿éµÄ·ÓÉÎļþ¡£ÔÚExampleÄ£¿éµÄRoutesĿ¼Ï£¬Ð½¨web.phpºÍapi.phpÁ½¸öÎļþ£¬²¢ÔÚÕâÁ½¸öÎļþÖл®·Ö½ç˵¸ÃÄ£¿éµÄǰ̨ºÍºǫ́·ÓÉ¡£
ÏÂÃæÊÇÒ»¸öʾÀý£º
//web.php Route::group(['module' => 'Example', 'middleware' => ['web']], function() { Route::get('/', 'ExampleController@index'); }); //api.php Route::group(['module' => 'Example', 'namespace' => 'AppModulesExampleControllersApi', 'prefix' => 'api/v1', 'middleware' => ['api']], function() { Route::get('/', 'ExampleApiController@index'); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬web.phpÎļþ½ç˵ÁËÒ»¸ögetÇëÇ󣬽«¸ùĿ¼”/”Ó³Éäµ½Example¿ØÖÆÆ÷µÄindexÒªÁìÉÏ¡£¶øapi.phpÎļþ½ç˵ÁËÒ»¸ögetÇëÇ󣬽«”/api/v1″Ó³Éäµ½ExampleApiController¿ØÖÆÆ÷µÄindexÒªÁìÉÏ¡£ÆäÖУ¬’module’²ÎÊýÖ¸¶¨Á˸÷ÓÉÊôÓÚExampleÄ£¿é£¬’middleware’²ÎÊýÖ¸¶¨Á˸÷ÓÉÒÀÀµÓÚwebÖÐÑë¼þ»òapiÖÐÑë¼þ£¬ÕâЩÖÐÑë¼þ¿ÉÒÔƾ֤ÏêϸÇéÐξÙÐÐÉèÖá£
Èý¡¢½¨Éè¿ØÖÆÆ÷
¿ØÖÆÆ÷ÊÇLaravelÖÐÓÃÓÚ´¦ÀíÓû§ÇëÇóµÄÒ»ÖÖ»úÖÆ¡£ÔÚExampleÄ£¿éµÄControllersĿ¼Ï£¬Ð½¨ExampleController.phpÎļþ£¬²¢½ç˵һ¸öindexÒªÁì×÷ΪʾÀý¡£
ʾÀý´úÂëÈçÏ£º
namespace AppModulesExampleControllers; use AppHttpControllersController; class ExampleController extends Controller { public function index() { return view('example::index'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ExampleControllerÀà¼ÌÐø×ÔLaravel¿ò¼ÜÌṩµÄControllerÀ࣬Ëü°üÀ¨Ò»¸öindexÒªÁ죬¸ÃÒªÁ콫ģ°åÎļþexample::index·µ»Ø¡£
ËÄ¡¢½¨ÉèÊÓͼÎļþ
ÔÚLaravelÖУ¬ÊÓͼÓÃÓÚÏÔʾHTMLÒ³Ãæ»òÆäËûÏìÓ¦Êý¾Ý¡£ÔÚExampleÄ£¿éµÄResourcesĿ¼Ï£¬Ð½¨ViewsĿ¼£¬²¢ÔÚÆäÖн¨Éèindex.blade.php£¬¸ÃÎļþÊÇÓÃÀ´×÷ΪÀý×ÓµÄÊÓͼÎļþ¡£
ʾÀý´úÂëÈçÏ£º
<!DOCTYPE html> <html> <head> <title>Example</title> </head> <body> <h1>Welcome to Example Module!</h1> </body> </html>
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬index.blade.phpÎļþÖ»°üÀ¨Ò»¸ö¼òÆӵĽӴýÓï¾ä£¬ÓÃÀ´ÌåÏÖÕâÊǸÃÄ£¿éµÄÊ×Ò³¡£
Îå¡¢½¨ÉèÄ£×ÓºÍЧÀÍÌṩÕß
ÔÚLaravelÖУ¬Ä£×ÓÓÃÓÚÓëÊý¾Ý¿â½»»¥£¬Í¨³£ÓÃÀ´·â×°Ò»ÕÅ±í¡£ÔÚExampleÄ£¿éµÄModelsĿ¼Ï£¬½¨ÉèExample.phpºÍExampleRepository.phpÁ½¸öÎļþ£¬»®·Ö´ú±íÄ£×ӺͲִ¢À࣬ËüÃǵÄ×÷Óû®·ÖÊÇÓëÊý¾Ý¿â½»»¥ºÍ·âװһЩÅÌÎÊÒªÁì¡£
ÔÚ½¨ÉèÍê³ÉÄ£×ӺͲִ¢ÀàÖ®ºó£¬ÐèÒªÔÚProvidersĿ¼Ï½¨ÉèÒ»¸öЧÀÍÌṩÕߣ¬ÓÃÓÚ×¢²áÄ£×ӺͲִ¢À࣬ͬʱʵÏÖЧÀÍÌṩÕßµÄregisterÒªÁì¡£
ʾÀý´úÂëÈçÏ£º
namespace AppModulesExampleProviders; use IlluminateSupportServiceProvider; class ExampleServiceProvider extends ServiceProvider { public function boot() { } public function register() { $this->app->bind('AppModulesExampleModelsExampleRepositoryInterface', 'AppModulesExampleModelsExampleRepository'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ExampleServiceProviderÀà¼ÌÐø×ÔLaravel¿ò¼ÜÌṩµÄServiceProviderÀ࣬ͨ¹ýregisterÒªÁì×¢²áÁËExampleRepository½Ó¿ÚºÍExampleRepositoryʵÏÖÀà¡£ÔÚÏÖʵʹÓÃʱ£¬¿ÉÒÔͨ¹ýÈÝÆ÷»ñÈ¡ExampleRepositoryʵÀý£¬È»ºóŲÓÃÆäÒªÁìÀ´ÊµÏÖÓëÊý¾Ý¿â½»»¥µÄ¹¦Ð§¡£
×ܽ᣺
Ä£¿é»¯±à³ÌÊÇÒ»ÖÖºÜÊÇÖ÷ÒªµÄ±à³ÌÍ·ÄÔ£¬ÔÚLaravelÖÐÒ²ÓÐ×ÅÓÅÒìµÄÌåÏÖ¡£Í¨¹ýÉÏÊöµÄ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÔÚLaravelÖÐÇáËɵؽ¨ÉèÒ»¸öÄ£¿é£¬²¢ÊµÏÖÓëÊý¾Ý¿âµÄ½»»¥ºÍÏêϸµÄÓªÒµÂß¼¡£ËäÈ»£¬ÔÚÏÖʵÏîÄ¿ÖУ¬Ä£¿éµÄÉè¼ÆÓëʵÏÖ²»Ö¹ÓÚ´Ë£¬ÐèҪ͎áÏêϸӪҵÐèÇó¾ÙÐкÏÀíÉè¼ÆºÍ¿ª·¢¡£
ÒÔÉϾÍÊÇlaravelн¨Ä£¿éÁ÷³ÌµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡