2015-03-24 2 views
5

Laravel 5 के बाद से, यह मेरे लिए ब्याज है - रजिस्टर करने के लिए कैसे और Laravel 5.लैरवेल 5 में पैकेज से कंसोल कमांड को कैसे पंजीकृत करें?

में पैकेज से कंसोल आदेश का उपयोग laracast में के रूप में https://laracasts.com/discuss/channels/tips/developing-your-packages-in-laravel-5 पर चर्चा, मैं एक निर्देशिका संरचना बनाने के लिए, autoload करने के लिए अपने पैकेज जोड़ सकते हैं और एक सेवा बनाने प्रदाता

<?php namespace Goodvin\EternalTree; 

use Console\InstallCommand; 
use Illuminate\Support\ServiceProvider; 

class EternalTreeServiceProvider extends ServiceProvider 
{ 
    public function boot() 
    { 

    } 

    public function register() 
    { 
     $this->registerCommands(); 
    } 

    protected function registerCommands() 
    { 
     $this->registerInstallCommand(); 
    } 

    protected function registerInstallCommand() 
    { 
     $this->app->singleton('command.eternaltree.install', function($app) { 

      return new InstallCommand(); 
     }); 
    } 

    public function provides() 
    { 
     return [ 

      'command.eternaltree.install' 
     ]; 
    } 
} 

मेरे InstallCommand.php स्क्रिप्ट/src/कंसोल

<?php namespace Goodvin\EternalTree\Console; 

use Illuminate\Console\Command; 
use Symfony\Component\Console\Input\InputOption; 
use Symfony\Component\Console\Input\InputArgument; 

class InstallCommand extends Command { 

    protected $name = 'install:eternaltree'; 
    protected $description = 'Command for EternalTree migration & model install.'; 

    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function fire() 
    { 
     $this->info('Command eternaltree:install fire'); 
    } 

} 

में संग्रहीत मैं app.php & में सेवा प्रदाता रजिस्टर पर अमल डंप-autoload। लेकिन जब मैं

php artisan eternaltree:install 

पर अमल करने की कोशिश यह मेरे

[InvalidArgumentException] There are no commands defined in the "eternaltree" namespace. 

दिखाने मुझे लगता है कि मेरी आदेश सेवा प्रदाता द्वारा पंजीकृत नहीं है, क्योंकि php कारीगर सूची मेरा आदेश नहीं दिखाती है। क्या कोई मुझे बता सकता है कि लैरवेल 5 में अपने पैकेज में कमांड को पंजीकृत करने का सही तरीका क्या है?

उत्तर

3

मैं एक निर्णय पाया है, यह आसान था:

registerInstallCommand() में

$this->commands('command.eternaltree.install'); 
18

जोड़े यह एक सेवा कंटेनर और एक सिंगलटन का उपयोग करने की आवश्यकता नहीं है। आप अपने कमांड क्लास को $this->commands([]); सरणी में अपने सेवा प्रदाता के register() अनुभाग में डाल सकते हैं।

कि जैसा

:

$this->commands([ 
    Acme\MyCommand::class 
]); 
+1

था के रूप में आप का सुझाव दिया: - लागू MyServiceProvider ServiceProvider तो '' 'सार्वजनिक समारोह रजिस्टर() फैली हुई है { // $ this-> आदेश ([ LaravelTypesController :: वर्ग ]); } '' - लैरावेल टाइप्स नियंत्रक $ हस्ताक्षर के साथ कमांड बढ़ाता है, $ विवरण और विधि को संभालता है, लेकिन फिर भी php artisan पर कमांड नहीं दिखता है। ग्रेप –

-1

बेहतर कभी नहीं से देर से।

आप eternaltree:install पर कॉल करने का प्रयास कर रहे थे लेकिन आपने install:eternaltree के रूप में आदेश पंजीकृत किया था।

आपका उत्तर सही है ... लेकिन registerCommands() और registerInstallCommands() विधियों की कोई आवश्यकता नहीं थी। register() विधि के भीतर से commands() विधि को कॉल करके अपना कोड दुबला रखें।

संबंधित मुद्दे