本文介绍了Laravel 5.3-是否可以通过单击链接/按钮来运行php artisan命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所言...当用户单击Web浏览器中的链接时,是否可以在后台运行php artisan命令?

So as the question says...is there a way to run php artisan command in background when user clicks on a link in web browser?

例如,我想在我的应用中创建一个用于迁移迁移文件的按钮,以便在单击此按钮时:

For example i would like to make a button in my app for migrating migration files so when this button is clicked:

<a href="/migrate" class="btn btn-primary">Migrate</a>

我想跑步

php artisan migrate

在后台.

这有可能吗?

推荐答案

可以!只需在您的routes\web.php文件中创建一条新路线.然后,您可以简单地调用Artisan::call()方法.

Sure you can! Just simply create a new route within your routes\web.php file. Then you can just simply call Artisan::call() method.

例如,当您访问make-migration路线时,您要为发票"表创建迁移文件.您可以这样操作:

For example, when you visit make-migration route, you want to create a migration file for Invoices table. You can do this like so:

Route::get('make-migration', function () {
    Artisan::call('make:migration', [
        'name' => 'create_invoices_table',
        '--create' => 'invoices',
    ]);

    return 'Create invoices migration table.';
});

或者在您的情况下,如果要运行迁移:

Or in your case, if you want to run the migration:

Route::get('migrate', function () {
    Artisan::call('migrate');

    return 'Database migration success.';
});

此处中了解更多有关以编程方式运行Artisan命令的信息.

Read more about running Artisan command programmatically here.

希望获得帮助!

这篇关于Laravel 5.3-是否可以通过单击链接/按钮来运行php artisan命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 08:13