【问题标题】:Is there a version of laravel for console app only? [closed]是否有仅适用于控制台应用程序的 laravel 版本? [关闭]
【发布时间】:2017-09-23 05:01:06
【问题描述】:

目前我在终端应用程序中使用 Symfony 控制台,但我发现 Laravel artisan 控制台有很多开箱即用的功能可供我使用。是否有没有用于开发 Web 应用程序的其他命令的 Laravel 版本?或者至少要删除安装 Laravel 时注册的默认可用命令?

【问题讨论】:

  • 您可以自行启动并运行illuminate/console
  • @ceejayoz 我一定会试试这个。谢谢!
  • Checkout this Link,您可以在 routes/console.php 中以 Laravel Routes 的形式定义控制台命令

标签: php laravel symfony laravel-artisan


【解决方案1】:

我刚刚使用illuminate/console

composer.json:

{
    "require": {
        "illuminate/console": "^5.4",
        "illuminate/container": "^5.4",
        "illuminate/events": "^5.4"
    },
    "autoload": {
        "psr-4": {"Yourvendor\\Yourproject\\": "src/"}
    }
}

your-console-app(替换为artisan):

#!/usr/bin/env php
<?php

use Illuminate\Console\Application;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;

use Yourvendor\Yourproject\Console\Commands\Yourcommand;

if (file_exists($a = __DIR__.'/../../autoload.php')) {
    require_once $a;
} else {
    require_once __DIR__.'/vendor/autoload.php';
}

$container = new Container;
$dispatcher = new Dispatcher;
$version = "5.4"; // Laravel version

$app = new Application($container, $dispatcher, $version);

$app->add(new Yourcommand);

$app->run();

src/Console/Commands/Yourcommand.php:

<?php

namespace Yourvendor\Yourproject\Console\Commands;

use Illuminate\Console\Command;

class Yourcommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'yourcommand:test {test}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Yourcommand test';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('Hello world!');
    }
}

使用以下命令运行控制台命令:

php your-console-app yourcommand:test

【讨论】:

  • 太棒了!即使我使用的是 python,我也一定会尝试一下。 :)
  • 我刚刚发现的一件事是似乎无法加载服务提供者,所以我刚刚从Illuminate\Console\Application切换到Illuminate\Foundation\Application,它具有所需的register()方法。
猜你喜欢
  • 2013-08-03
  • 1970-01-01
  • 2018-04-12
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多