Laravel7ããªãªã¼ã¹ããã¦ããã£ããããã使ã£ãéçºã®æ©ä¼ããã£ã¦ãã¾ããã®ã§ã使ç¨é »åº¦ã®é«ããã®ãã¾ã¨ãã¦ããããã¨æãã¾ãã
5ç³»ã6ç³»ã®æ å ±ã¯å¤ãåºåã£ã¦ãã¾ããã6ç³»ãLTSã¨ãããã¨ããã£ã¦ï¼ç³»ã®æ å ±ã¯å°ãªãã§ããã
Â
ä»åã¯ãLaravel7ç³»ã§Traitã®makeã³ãã³ããä½æããæ¹æ³ãããç´¹ä»ãã¾ãã
Â
ã»ç°å¢
PHPï¼7.2.5
Laravelï¼7.18.0
ã»åèãã¼ã¸
 Â
Traitã¸ã§ãã¬ã¼ã¿ã¼ã®ä½æ
Â
Traitã¸ã§ãã¬ã¼ã¿ã¼ãä½æããããã«ã¯ã以ä¸ã®ï¼ã¹ãããã§è¡ãã¾ãã
- ã³ãã³ãã®æºå
- ãã³ãã¬ã¼ãï¼.stubï¼ã®æºå
- Traitã®ä½æ
Â
ã§ã¯æé ã«æ²¿ã£ã¦èª¬æãã¦ããã¾ãã
Â
Â
ã³ãã³ãã®æºå
Â
ã³ã³ããã¼ã©ã¼ããªã¯ã¨ã¹ãã¨åãããã«makeã³ãã³ãã§ãã¬ã¤ããä½æããããã«ã¯ããã¬ã¤ãç¨ã®makeã³ãã³ããä½æããªããã°ããã¾ããã
ã¾ãã¯Commandã¯ã©ã¹ãç¨æããããã«ãLaravelãã£ã¬ã¯ããªã§ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¦ãã ããã
$ php artisan make:command TraitMakeCommand
Â
ããã§ã以ä¸ã®ãã£ã¬ã¯ããªã«TraitMakeCommand.phpãä½æããã¾ãã
app/Console/Commands
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TraitMakeCommand extends Command
{
   /**
    * The name and signature of the console command.
    *
    * @var string
    */
   protected $signature = 'command:name';
   /**
    * The console command description.
    *
    * @var string
    */
   protected $description = 'Command description';
   /**
    * Create a new command instance.
    *
    * @return void
    */
   public function __construct()
   {
       parent::__construct();
   }
   /**
    * Execute the console command.
    *
    * @return mixed
    */
   public function handle()
   {
       //
   }
}
Â
ã³ãã³ãå®è¡ç´å¾ã®TraitMakeCommand.phpã¯ä¸è¨ã®ãããªå 容ã«ãªã£ã¦ãã¾ãã®ã§ã以ä¸ã®ããã«æ¸ãæãã¦ãã ããã
<?php
namespace App\Console\Commands;
use Illuminate\Console\GeneratorCommand as Command;
class TraitMakeCommand extends Command
{
             /**
             * The name and signature of the console command.
             *
             * @var string
             */
             protected $signature = 'make:trait {name}';
             /**
             * The console command description.
             *
             * @var string
             */
             protected $description = 'Create a new trait';
             /**
             * The type of class being generated.
             *
             * @var string
             */
             protected $type = 'Trait';
             /**
             * Get the stub file for the generator.
             *
             * @return string
             */
             protected function getStub()
             {
                           base_path('/stubs/trait.stub');
             }
             /**
             * Get the default namespace for the class.
             *
             * @param string $rootNamespace
             * @return string
             */
             protected function getDefaultNamespace($rootNamespace)
             {
                           return $rootNamespace.'\Traits';
             }
}
 â»Laravelï¼ãï¼ç³»ã§ã¯$signatureã«å ¥ãã¦ããã³ãã³ãåã®{name}ãä¸è¦ã ã£ãã®ã§ããããã¡ããæãã¦ããã¨make:traitãå®è¡ãã¦ããThe “name” argument does not exist.ãã®ã¨ã©ã¼ãåãããã®ã§ï¼ç³»ã§ã¯è¿½å ãã¾ãã
Â
Â
ãã³ãã¬ã¼ãï¼.stubï¼ã®æºå
Â
æ¸ãæããTraitMakeCommand.phpå ã«è¨è¿°ãã¦ããããã«ããã³ãã¬ã¼ãï¼.stubï¼ã®ãã¡ã¤ã«ã以ä¸ã®ãã£ã¬ã¯ããªã¸ç¨æãã¾ãã
Laravelã«ã¼ã/stubs
â»Laravel7ç³»ããã¯php artisan stub:publishã§RequestãControllerãªã©æ¨æºã§æè¼ããã¦ããmakeã³ãã³ãç¨ã®stubãã«ã¹ã¿ã ã§ããããã«ãªã£ãããããã®æ ¼ç´å ã¨åããLaravelç´ä¸ã®stubsãã£ã¬ã¯ããªãä¿åå ã¨ãã¦ãã¾ã
Â
trait.stubã¨ãããã¡ã¤ã«åã§ä½æãã以ä¸ã®ããã«è¨è¿°ãã¦ãã ããã
<?php
namespace {{ namespace }};
trait {{ class }}
{
   //
}
Â
ããã§ãã³ãã¬ã¼ããã¡ã¤ã«ã®æºåã¯å®äºã§ãã
Â
Â
Traitã®ä½æ
Â
ã³ãã³ãã¨ãã³ãã¬ã¼ããæºåã§ããã°ãæ£å¸¸ã«ã³ãã³ããç»é²ããã¦ããããlistã³ãã³ãã使ã£ã¦ç¢ºèªãã¦ã¿ã¦ãã ããã
$ php artisan list | grep make
make
 make:channel                   Create a new channel class
 make:command                   Create a new Artisan command
 make:controller                Create a new controller class
 make:event                     Create a new event class
 make:exception                 Create a new custom exception class
 make:factory                   Create a new model factory
 make:job                       Create a new job class
 make:listener                  Create a new event listener class
 make:mail                      Create a new email class
 make:middleware                Create a new middleware class
 make:migration                 Create a new migration file
 make:model                     Create a new Eloquent model class
 make:notification              Create a new notification class
 make:observer                  Create a new observer class
 make:policy                    Create a new policy class
 make:provider                  Create a new service provider class
 make:request                   Create a new form request class
 make:resource                  Create a new resource
 make:rule                      Create a new validation rule
 make:seeder                    Create a new seeder class
 make:test                      Create a new test class
 make:trait                      Create a new trait
Â
æçµè¡ã«make:traitã¨ããè¨è¿°ãããã°æ£å¸¸ã«ä½æããã¦ãã¾ãã
ã³ãã³ããä½æãããããmake:traitã使ã£ã¦ãã¬ã¤ããä½æãã¾ãã
$php artisan make:trait TestTrait
Â
æ£å¸¸ã«çµäºããã°ãTestTrait.phpã追å ããã¦ããã¯ãã§ãã
app/Traits/TestTrait.php
<?php
namespace App\Traits;
trait TestTrait
{
   //
}
Â
ããã§Laravel7ç³»ã§ãã¬ã¤ãç¨ã®makeã³ãã³ããä½æãããã¨ãã§ãã¾ããã
Â
Â
ã¾ã¨ã
Â
ãããã ã£ãã§ããããã
ä»åã¯ãLaravel7ç³»ã§Traitã®makeã³ãã³ããä½æããæ¹æ³ãããç´¹ä»ãã¾ããã
ä»ã«ãèªä½ã§makeã³ãã³ããä½æããã人ã¯ãåãããæ¹ã§ã§ãã¾ãã®ã§è©¦ãã¦ã¿ã¦ãã ããã
Laravel7ç³»ã®æ å ±ã¯5ã6ã¨æ¯ã¹ãã¨ã¾ã ã¾ã å°ãªãã§ãã®ã§ãããããããLaravelï¼ç³»ã使ã£ãéçºãèãã¦ãã人ã¯ããã²åèã«ãã¦ãã ãããã
Laravel使ã£ã¦éçºãããªãèªãã§ããã¦æã¯ãªããï¼ å ¥éã¨ããåã®éããæåã«èªãã¹ãï¼åããPHPã®æä½éã®ç¥èã¯å¿ è¦ãããããããã¾ã ååããç¡ã人ã¯PHPã®å ¥éæ¸ãå®è·µæ¸ã«ç®ãéãããã§ã Viewã®ä½¿ãæ¹ãbladeã®è¨è¿°æ¹æ³ã¨ã詳ããè¼ã£ã¦ãããã使ã£ããã¨æããã©è©³ããã¯ç¥ãã人ã«ã¨ã£ã¦ãããã£ï¼ä¾¿...