Artisan commands with HTTP
4 | 5 | 6 | --- 7 | 8 |       9 | 10 |  11 | 12 | **There might be some times you wanted to execute an Artisan command, but you did not have access to shell or SSH. 13 | Here we brought REST API solution for you.** 14 | 15 | **You are able to run Artisan commands by REST APIs easily.** 16 | 17 | *'README.md need to be updated'* 18 | 19 | ### Table of contents 20 | - **[Get Started](#get-started)** 21 | - **[Endpoints](#endpoints)** 22 | - **[Routes](#routes)** 23 | - **[Responses](#responses)** 24 | - **[Successful](#successful)** 25 | - **[Not Found](#not-found)** 26 | - **[Invalid Arguments format](#invalid-arguments-format)** 27 | - **[Invalid Options format](#invalid-options-format)** 28 | - **[Forbidden Routes](#forbidden-routes)** 29 | - **[Authentication](#authentication)** 30 | - **[Configurations](#configurations)** 31 | - **[API Prefix and HTTP Method](#api-prefix-and-http-method)** 32 | - **[Auto Run](#auto-run)** 33 | - **[Security](#security)** 34 | - **[Middlewares](#middlewares)** 35 | - **[Useful tips](#useful-tips)** 36 | - **[Todo](#todo)** 37 | 38 | 39 | ### Get Started 40 | 41 | To use this package, you should install it alongside [Laravel v9.21](https://laravel.com/) and [PHP v8.0](https://php.net) or higher. 42 | 43 | you can install it via [Composer package manager](https://getcomposer.org/): 44 | ```shell 45 | composer require aariow/artisan-api --dev 46 | ``` 47 | 48 | Although, Artisan-Api has production decetor itself, it is possible to install it globally. 49 | 50 | ### Endpoints 51 | 52 | As its name explains, all commands are available via HTTP with _POST_ method. 53 | All commands will be generated as routes and integrated into Laravel Routing system. You only need to send a POST request and follow the signature, like other REST API endpoints. 54 | 55 | There are two kinds of commands; one is normal commands like `php artisan list` or `php artisan cache:clear`, and the second form is `GeneratorCommands` which tend to create files within your application like `make:model` and `make:migration`. These kind of commands have different purposes, you should follow diffenerent convention. 56 | 57 | > **`GeneratorCommand`** are instance of **`Illuminate\Console\GeneratorCommand`** that extends **`Illuminate\Console\Command`** class. 58 | 59 | All commands existed by default or created by you will be discovered automatically and you do not have to do anything manually. Thus, their endpoints will be generated dynamically to your application. So if you delete/add any command class, there is no reason to worry. 60 | 61 | #### Routes 62 | 63 | Let's dive into using endpoints: 64 | 65 | Routes are generated with the following format: 66 | ```shell 67 | https://domain.com/artisan/api/{command}/{subcommand}?args=key1:value1,key2:value2&options=opt1:value1,opt2 68 | ``` 69 | \ 70 | So the above endpoint will be translated to: 71 | ```shell 72 | php artisan command:subcommand value1 value2 --opt1=value1 --opt2 73 | ``` 74 | \ 75 | And for **Generator** commands the endpoint is: 76 | ```shell 77 | https://domain.com/artisan/api/{command}/{subcommand}/{name}?args=key1:value1,key2:value2&options=opt1:value1,opt2 78 | ``` 79 | 80 | **Pay attention that there is a `name` variable. As all Generator commands need an argument called `name`, this needs to be specified by what you desire.** 81 | 82 | \ 83 | Command Examples: 84 | 85 | ```shell 86 | php artisan list 87 | ``` 88 | will be translated to: 89 | ```shell 90 | https://domain.com/artisan/api/list 91 | ``` 92 | \ 93 | and this: 94 | ```shell 95 | php artisan cache:forget myCachedKeyName 96 | ``` 97 | will be translated to: 98 | ```shell 99 | https://domain.com/artisan/api/cache/forget?args=key:myCachedKeyName 100 | ``` 101 | \ 102 | Another one: 103 | ```shell 104 | php artisan optimize:clear -v 105 | ``` 106 | will be translated to: 107 | ```shell 108 | https://domain.com/artisan/api/optimize/clear?options=v 109 | ``` 110 | \ 111 | A **Generator** one: 112 | ```shell 113 | php artisan make:model MyModel --controller -f 114 | ``` 115 | will be translated to: 116 | ```shell 117 | https://domain.com/artisan/api/make/model/MyModel?options=controller,f 118 | ``` 119 |