Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel PHP artisan命令-尝试使用来自调用类的输出命令_Php_Laravel_Laravel 5.1 - Fatal编程技术网

Laravel PHP artisan命令-尝试使用来自调用类的输出命令

Laravel PHP artisan命令-尝试使用来自调用类的输出命令,php,laravel,laravel-5.1,Php,Laravel,Laravel 5.1,我正在尝试编写artisan命令。 我已经掌握了基本知识 现在,我正试图通过将一些代码移动到另一个文件来清理问题 问题是,在另一个文件中,像$this line('hello')这样的命令不起作用 有没有一种简单的方法可以做到这一点 (下面有两个文件,第一个文件是命令,它可以工作。 请注意,在“工作”文件的底部,我们是这样做的 $tmp=newviewclass然后$tmp->display() 第二个文件是我想要放置所有逻辑的地方——如何从第二个文件调用继承的函数,如$this->line、$

我正在尝试编写artisan命令。 我已经掌握了基本知识

现在,我正试图通过将一些代码移动到另一个文件来清理问题

问题是,在另一个文件中,像
$this line('hello')
这样的命令不起作用

有没有一种简单的方法可以做到这一点

(下面有两个文件,第一个文件是命令,它可以工作。 请注意,在“工作”文件的底部,我们是这样做的
$tmp=newviewclass
然后
$tmp->display()

第二个文件是我想要放置所有逻辑的地方——如何从第二个文件调用继承的函数,如$this->line、$this->info、$this->table等等

CrudFromDb\u 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
    }
}
<?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应该已经有了一种内置的方法来实现这一点了?)

ie在
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('text'),但不起作用:-(

在viewclass.php中添加下面的名称空间,并允许viewclass扩展命令

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');
    }
}

在viewclass.php中添加以下名称空间,并允许viewclass扩展命令

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');
    }
}
这个怎么样

<?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
    }
}
这个怎么样

<?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
    }
}

您遇到的错误是什么?我想您应该尝试使用$tmp=new viewclass();您好Ranakrunal9,非常感谢您查看我的问题!新类的调用很好-如果我在display函数中添加了echo,它就可以工作了,所以我不认为该行中缺少()错误(尽管我刚才尝试了,但没有工作)“PHP致命错误:调用undefined方法….viewclass::line()”显然,它在我的viewclass中查找函数,而不是从命令类中提取函数。我尝试过parent::line()但行不在直接父级中,它在Console中处于两个级别-有没有关于如何调用它的想法?您是否没有收到任何错误,因为您的
viewclass
没有
use illighted\Console\Command;
语句来标识它扩展的
命令
类?您收到了哪个错误?我认为您应该这样做尝试使用$tmp=new viewclass();您好Ranakrunal9,非常感谢您查看我的问题!新类的调用很好-如果我在display函数中添加一个echo,它就可以工作了,所以我不认为这是该行中缺少的()(尽管我刚才尝试了,但没有工作),错误是“PHP致命错误:调用undefined方法….viewclass::line()“显然,它在我的viewclass中查找函数,而不是从命令类中提取函数。我尝试了parent::line()但行不在直接父级中,它在Console中有两个级别-有没有关于如何调用它的想法?您是否没有收到任何错误,因为您的
viewclass
没有
use-illighted\Console\Command;
语句来标识它扩展的
命令
类?我尝试了,也尝试了extends命令-没有这两种方法都很幸运。我认为问题在于
illumb\Console\Command
在构造函数中有一些设置环境的东西。我尝试添加
parent::\uu construct()
在我的viewclass的
\uuu contstruct
中,但是它确实在尝试设置一个全新的laravel/artisan命令,我已经在
CrudFromDb\u view.php文件中完成了这项工作:-(我已经更新了我的答案,请检查它是否可以解决问题:)仍然不走运:PHP致命错误:
调用成员函数writeln()在第342行的/home/vagrant/Code/Laravel/vendor/Laravel/framework/src/illumb/Console/Command.php中的null上,我尝试了这一点,也尝试了extends命令-两者都没有成功。我认为问题在于illumb\Console\Command在构造函数中有一些设置环境的东西。我尝试添加
parent::\uuuu construct()
在我的viewclass的
\uuuu contstruct
中,但是它确实在尝试设置一个全新的laravel/artisan命令,我已经在
CrudFromDb\u view.php
文件中这样做了:-(我已经更新了我的答案,请检查它是否可以解决问题:)仍然不走运:PHP致命错误:
在第342行的/home/vagrant/code/Laravel/vendor/Laravel/framework/src/illumb/Console/Command.PHP中调用null上的成员函数writeln()