在Laravel 4路线中运行Artisan命令


Run Artisan Command in a route Laravel 4

我在路由上运行artisan命令时遇到问题。

这是代码:

Route::get('/create_preview', function() {
    Artisan::call('cartero:run-phantomjs', array('url' => 'http://stackoverflow.com/'));
});

我的命令接受url作为参数,我使用PhantomJS从给定的网站创建屏幕截图。

在终端上运行得很好,但我的浏览器调用路由时没有发生任何事情。

有什么想法吗?

有这个命令:

Usage:
 firewall:blacklist [--force] ip
Arguments:
 ip                    The IP address to be added.
Options:
 --force               Remove IP before adding it to the list.
 --help (-h)           Display this help message.
 --quiet (-q)          Do not output any message.
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version.
 --ansi                Force ANSI output.
 --no-ansi             Disable ANSI output.
 --no-interaction (-n) Do not ask any interactive question.
 --env                 The environment the command should run under.

这对我有效:

Artisan::call('firewall:blacklist', ['ip' => '10.10.10.10']);

你说but nothing happens from my browser calling the route.。没错,浏览器中不会显示任何内容,因为这是一个"仅命令行"命令,终端打印的字符串、回声等不会在Artisan::call()中发送到浏览器,甚至不会发送错误。

试试这个:

Artisan::call('cartero:run-phantomjs', array('--url' => 'http://stackoverflow.com/'));

这是服务器中路径的问题,下面是我的命令:

public function fire()
{
  $url = $this->argument('url');
  $filename = uniqid("sc");
  $path_phantomjs = base_path() . "/phantomjs/bin/phantomjs";
  $path_js = base_path() . "/screen.js";
  $path_dest = base_path() . "/public/uploads/previews/".$filename.".png";
  return exec($path_phantomjs . " " .$path_js . " " . $url . " " .$path_dest);
}

最初我没有使用helper base_path(),它在终端上运行良好,但在浏览器中运行不好。我添加了一条路径,现在就可以工作了!!!!