I'm trying to write an artisan command. I've got the basics working.
Now I'm trying to clean things up by moving some of the code to another file.
The trouble is, in that other file, commands like $this-line('hello') don't work.
Is there an easy way to make that work?
(two files below, first file is the command and it works.
note in the bottom of the 'working' file, we do
$tmp = new viewclass then $tmp->display()
The second file is where I want to put all the logic - how do I call inherited functions like $this->line, $this->info, $this->table, etc... from that second file?
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');
}
}
Note, one approach that worked, was to pass the object in.
(It feels like PHP should have a built in way to do this already?)
ie in CrudFromDb_view.php:
$tmp = new viewclass($this);
$tmp->display()
and in viewclass.php
class viewclass
{
protected $myparent;
function __construct($thisfromparent)
{
$this->myparent = $thisfromparent
}
function display()
{
$this->myparent->line('this works');
}
}
Is there a more elegant way to do what's been done above? I've tried parent::line('text') but that doesn't work :-(
viewclassdoesn't have theuse Illuminate\Console\Command;statement to identify theCommandclass it's extending?