现在我试图通过将一些代码移动到另一个文件来清理它。
问题是,在其他文件中,像$this-line('hello')
这样的命令不起作用。
有没有一种简单的方法可以让它发挥作用?
(下面有两个文件,第一个文件是命令,它可以工作。
请注意“工作”的底部。文件,我们做
$tmp = new viewclass
然后$tmp->display()
第二个文件是我要放置所有逻辑的地方 - 如何调用继承的函数,如$ this-> line,$ this-> info,$ this-> table等...来自那个第二档?
http://laravel.com/docs/5.1/artisan#writing-output
CrudFromDb_view.php:
<?php
namespace path\laravel_crudfromdb\Commands;
use Illuminate\Console\Command;
use \path\laravel_crudfromdb\Classes\viewclass;
class CrudFromDb_Views extends Command
{
protected $signature = 'z:viewviews';
protected $description = 'Displays generated views on screen. Does not change or create any files';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->line(' THIS LINE WORKS');
$tmp = new viewclass;
$tmp->display(); // <- Fails, see 'viewclass.php' file below
}
}
viewclass.php:
<?php
namespace path\laravel_crudfromdb\Classes;
class viewclass extends Command
{
protected $env;
protected $dbhost;
protected $dbname;
protected $dbuser;
protected $dbpw;
protected $connection;
function __construct()
{
// parent::__construct();
}
function display()
{
//this fails!
//how can I call this 'line' function?
$this->line('This is a line');
}
}
(感觉PHP应该有内置的方法来实现这一点吗?)
即CrudFromDb_view.php
:
$tmp = new viewclass($this);
$tmp->display()
并在viewclass.php
class viewclass
{
protected $myparent;
function __construct($thisfromparent)
{
$this->myparent = $thisfromparent
}
function display()
{
$this->myparent->line('this works');
}
}
是否有更优雅的方式来做上面做过的事情? 我已尝试过parent :: line(&#39; text&#39;)但这不起作用: - (
答案 0 :(得分:2)
在viewclass.php中添加以下命名空间,并允许viewclass扩展Command。
use Illuminate\Console\Command;
class viewclass extends Command
{
protected $env;
protected $dbhost;
protected $dbname;
protected $dbuser;
protected $dbpw;
protected $connection;
function __construct()
{
//parent::__construct();
}
function display()
{
//this fails!
//how can I call this 'line' function?
$this->line('This is a line');
}
}
答案 1 :(得分:2)
这个怎么样?
<?php
namespace path\laravel_crudfromdb\Commands;
use Illuminate\Console\Command;
use \path\laravel_crudfromdb\Classes\viewclass;
class CrudFromDb_Views extends Command
{
protected $signature = 'z:viewviews';
protected $description = 'Displays generated views on screen. Does not change or create any files';
public function handle()
{
$this->line('THIS LINE WORKS');
$tmp = app()->make(viewclass::class);
$tmp->display(); // Should work
}
}
使用app()->make()
将确保Laravel实现父Command
类中所需的所有依赖注入。这是Laravel的核心目的,它首先是IoC(控制反转)容器。
要使其更加“犹太洁食”,请将您的viewclass
类添加为依赖注入...
<?php
namespace path\laravel_crudfromdb\Commands;
use Illuminate\Console\Command;
use \path\laravel_crudfromdb\Classes\viewclass;
class CrudFromDb_Views extends Command
{
protected $signature = 'z:viewviews';
protected $description = 'Displays generated views on screen. Does not change or create any files';
protected $viewclass;
public function __construct(viewclass $viewclass)
{
$this->viewclass = $viewclass;
parent::__construct(); //put it as the last line to avoid constructor bugs.
}
public function handle()
{
$this->line('THIS LINE WORKS');
$this->viewclass->display(); // Should still work thanks to IoC Container
}
}