Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel ArtisanÓÅ»¯¿ª·¢ÌåÑ飿
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶àÓÅÒìµÄ¹¤¾ßÀ´×ÊÖúÌá¸ß¿ª·¢Ð§ÂÊ¡£ÆäÖУ¬laravel artisanÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ¹¤¾ß£¬Ëü¿ÉÒÔÉÁ¿ª·¢ÕßÔ½·¢±ã½ÝµØÍê³ÉÐí¶àÊÂÇé¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃlaravel artisanÀ´ÓÅ»¯¿ª·¢ÌåÑé¡£
Ò»¡¢Ê²Ã´ÊÇLaravel Artisan£¿
Laravel ArtisanÊÇLaravel¿ò¼ÜÖеÄÏÂÁîÐй¤¾ß¡£Ëü¿ÉÒÔ×ÊÖú¿ª·¢ÕßÍê³ÉÐí¶àÖÖÖÖʹÃü£¬°üÀ¨Êý¾Ý¿âǨáã¡¢ÌìÉú´úÂëÎļþ¡¢ÔËÐе¥Î»²âÊԵȡ£Ê¹ÓÃLaravel Artisan£¬²»µ«¿ÉÒÔÌá¸ß¿ª·¢Ð§ÂÊ£¬²¢ÇÒ¿ÉÒԹ淶¿ª·¢Á÷³Ì¡£
¶þ¡¢ÔõÑùʹÓÃLaravel Artisan£¿
Laravel ArtisanÊÇÒ»¸öºÜÊÇÇ¿Ê¢µÄÏÂÁîÐй¤¾ß¡£ÒÔÏÂÊÇһЩʹÓÃLaravel ArtisanµÄʾÀý£º
1¡¢ÌìÉú¿ØÖÆÆ÷
Òª½¨ÉèÒ»¸ö¿ØÖÆÆ÷£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan make:controller UserController
µÇ¼ºó¸´ÖÆ
ÆäÖÐUserControllerÊÇ¿ØÖÆÆ÷µÄÃû³Æ¡£Ö´ÐÐÒÔÉÏÏÂÁîºó£¬Laravel»áÔÚ/app/Http/ControllersĿ¼ÏÂÌìÉúÒ»¸öUserController.phpÎļþ£¬¸ÃÎļþÊÇÒ»¸ö¿ÕµÄ¿ØÖÆÆ÷Àà¡£
2¡¢ÌìÉúÄ£×Ó
Òª½¨ÉèÒ»¸öÄ£×Ó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan make:model User
µÇ¼ºó¸´ÖÆ
ÆäÖÐUserÊÇÄ£×ÓµÄÃû³Æ¡£Ö´ÐÐÒÔÉÏÏÂÁîºó£¬Laravel»áÔÚ/appĿ¼ÏÂÌìÉúÒ»¸öUser.phpÎļþ£¬¸ÃÎļþÊÇÒ»¸ö¿ÕµÄÄ£×ÓÀà¡£
3¡¢ÌìÉúǨáã
Òª½¨ÉèÒ»¸öǨá㣬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan make:migration create_users_table --create=users
µÇ¼ºó¸´ÖÆ
ÆäÖÐcreate_users_tableÊÇǨáãÎļþµÄÃû³Æ£¬–create=usersÌåÏÖÒª½¨ÉèÒ»¸öÃûΪusersµÄ±í¡£Ö´ÐÐÒÔÉÏÏÂÁîºó£¬Laravel»áÔÚ/database/migrationsĿ¼ÏÂÌìÉúÒ»¸öǨáãÎļþ£¬¸ÃÎļþ°üÀ¨up()ºÍdown()Á½¸öÒªÁ죬¿ÉÒÔÔÚÆäÖÐÌí¼Ó½¨ÉèºÍɾ³ý±íµÄ²Ù×÷¡£
4¡¢ÔËÐÐǨáã
ÒªÔËÐÐÊý¾Ý¿âǨá㣬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan migrate
µÇ¼ºó¸´ÖÆ
´ËÏÂÁִÐÐ/database/migrationsĿ¼ÏµÄËùÓÐδִÐеÄǨáãÎļþ£¬²¢½«¼Í¼²åÈëLaravelǨáã±íÖС£
5¡¢ÌìÉúSeeder
Òª½¨ÉèÒ»¸öSeeder£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan make:seeder UsersTableSeeder
µÇ¼ºó¸´ÖÆ
ÆäÖÐUsersTableSeederÊÇSeederµÄÃû³Æ¡£Ö´ÐÐÒÔÉÏÏÂÁîºó£¬Laravel»áÔÚ/database/seedsĿ¼ÏÂÌìÉúÒ»¸öUsersTableSeeder.phpÎļþ£¬¸ÃÎļþÊÇÒ»¸öÀ࣬¿ÉÒÔÔÚÆäÖÐÌí¼Ó²åÈëÊý¾ÝµÄ²Ù×÷¡£
6¡¢ÔËÐÐSeeder
ÒªÔËÐÐSeeder£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
php artisan db:seed
µÇ¼ºó¸´ÖÆ
´ËÏÂÁִÐÐ/database/seedsĿ¼ÏµÄËùÓÐSeederÀ࣬²¢½«¼Í¼²åÈëÊý¾Ý¿âÖС£
ÒÔÉÏÊÇ×î³£¼ûµÄLaravel ArtisanÏÂÁîʾÀý£¬ËäÈ»ËüÃÇÖ»ÊÇLaravel Artisan¶¥²ãÏÂÁîµÄÒ»²¿·Ö¡£Í¨¹ýÉó²é Laravel Îĵµ£¬ÎÒÃÇ¿ÉÒÔÏàʶµ½ÉÐÓиü¶àµÄÏÂÁî¿ÉÒÔ×ÊÖúÎÒÃÇÍê³ÉÔ½·¢ÖØ´óµÄʹÃü¡£
Èý¡¢ÔõÑù×Ô½ç˵Laravel ArtisanÏÂÁ
³ýÁËÄÚÖÃÏÂÁîÖ®Í⣬Äú»¹¿ÉÒÔ×Ô½ç˵Laravel ArtisanÏÂÁî¡£×Ô½ç˵ÏÂÁîÓÐÒÔÏÂÀûÒ棺
1¡¢¿ÉÒÔ¸üºÃµØÓÅ»¯¿ª·¢ÌåÑé
¿ÉÒÔƾ֤×Ô¼ºµÄÐèÒª£¬×Ô½ç˵һЩÏÂÁÒÔ¼ò»¯Ä³Ð©²Ù×÷£¬´Ó¶ø¸üºÃµØÓÅ»¯¿ª·¢ÌåÑé¡£
2¡¢¿ÉÒÔÈñðÈ˸üºÃµØÃ÷È·Ä£¿é
¹ØÓÚÒ»¸ö´óÐ͵ÄÏîÄ¿£¬ÓÐÐí¶àµÄÄ£¿é£¬ÈôÊÇʹÓÃLaravel ArtisanÀ´ÖÎÀíÿ¸öÄ£¿é£¬²»µ«¿ÉÒÔ¸üºÃµØ×éÖ¯´úÂ룬²¢ÇÒ¿ÉÒÔÈñðÈ˸üºÃµØÃ÷È·Ä£¿é¡£
ÏÂÃæÊÇÔõÑù×Ô½ç˵Laravel ArtisanÏÂÁîµÄ°ì·¨£º
1¡¢ÔÚ/app/Console/CommandsĿ¼Ï½¨ÉèÒ»¸öеÄPHPÀ࣬¼ÌÐøIlluminateConsoleCommandÀ࣬²¢½ç˵һ¸öÃû³ÆΪhandleµÄÒªÁì¡£
namespace AppConsoleCommands; use IlluminateConsoleCommand; class MyCommand extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name'; /** * The console command description. * * @var string */ protected $description = 'My custom command'; /** * Execute the console command. * * @return int */ public function handle() { // your code here } }
µÇ¼ºó¸´ÖÆ
2¡¢ÔÚ$signatureÊôÐÔÖÐÖ¸¶¨ÃüÁîµÄÃû³ÆºÍÑ¡Ïî¡£ÀýÈ磺
protected $signature = 'command:name {--option : description}';
µÇ¼ºó¸´ÖÆ
3¡¢ÔÚhandleÒªÁìÖбàдÏÂÁîÖ´ÐеĴúÂë¡£
4¡¢½«×Ô½ç˵ÏÂÁî×¢²áµ½Laravel ArtisanÖС£ÔÚ/app/Console/Kernel.phpÎļþÖÐregisterÒªÁìÖÐÌí¼ÓÒÔÏ´úÂ룺
protected $commands = [ CommandsMyCommand::class, ]; /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); }
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬Äú¾Í¿ÉÒÔÔÚÖÕ¶ËÖÐʹÓÃÒÔÏÂÏÂÁîÖ´ÐÐ×Ô½ç˵ÏÂÁ
php artisan command:name
µÇ¼ºó¸´ÖÆ
×ܽá
±¾ÎÄÏÈÈÝÁËLaravel ArtisanµÄ»ù±¾ÖªÊ¶ºÍʹÓ÷½·¨£¬ÒÔ¼°ÔõÑù×Ô½ç˵Laravel ArtisanÏÂÁϣÍûͨ¹ý±¾ÎĵÄÏÈÈÝ£¬Äú¿ÉÒÔ¸üºÃµØÏàʶºÍÕÆÎÕLaravel ArtisanµÄʹÓü¼ÇÉ£¬½ø¶øÌá¸ß¿ª·¢Ð§ÂÊ¡£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel ArtisanÓÅ»¯¿ª·¢ÌåÑ飿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡