Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/monkenWu/CodeIgniter4_CLI-Create

Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4.
https://github.com/monkenWu/CodeIgniter4_CLI-Create

Last synced: about 2 months ago
JSON representation

Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4.

Awesome Lists containing this project

README

        

# CodeIgniter4_CLI-Create

[![Latest Stable Version](https://poser.pugx.org/monken/cli-create/v)](//packagist.org/packages/monken/cli-create) [![Total Downloads](https://poser.pugx.org/monken/cli-create/downloads)](//packagist.org/packages/monken/cli-create) [![License](https://poser.pugx.org/monken/cli-create/license)](//packagist.org/packages/monken/cli-create)

Cli-Create is based on CodeIgniter4. It will help you generate template files more quickly when developing projects with CodeIgniter4.

[中文使用說明](https://hackmd.io/@monkenWu/ByZF1n4HL)

[Guide](https://hackmd.io/@monkenWu/HJndHeESU)

## Install

### Prerequisites
1. CodeIgniter Framework 4.*
2. Composer

### Composer Install

```
composer require monken/cli-create
```
### Use Library

Open Terminal in Mac/Linux or go to Run > “cmd” in Windows and navigate to CodeIgniter4 project’s root:

```
php spark list
```

Now, if you see the following message, the installation is successful.

```
CodeIgniter CLI Tool - Version 4.0.2 - Server-Time: 2020-03-09 12:04:21pm

Cli-Create
create:controller Create a new controller file.
create:model Create a new model file.
```

# Guide

## create:controller

Create a new controller file.

* Use
```
$ php spark create:controller [controller_name] [Options]
```

* Description:
```
Create a new controller file.
```
* Arguments:
1. controller_name : The controller name.

* Options:
```
-nobase Do not extends BaseControllers Class.
-usemodel Choose models.
-space Create folders and files according to the path you typed.
```

### Create a normal Controller

You can use :

```
$ php spark create:contorller [controller_name]
```

Now, in "app/Controllers" You can see the new Contorller File like this :

```php
The namespace in Codeigniter usually maps the actual file storage path, so using this command will automatically create folders according to the value you entered.

Now, in “app/Controllers/System/Admin” You can see the new "-nobase" Controller File like this :

```php
respond([
"status" => true,
"msg" => "index method successful."
]);
}

/*****/
```

Then, go to "app / Config / Routes.php" and you will see that the routing settings for this Controller have been registered in your configuration file :

```php
/**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

//CliCreate-add-in-2020-04-10 05:42:27
$routes->resource('user',[
'controller' =>'\App\Controllers\User',
]);
```

> Please note that the function of automatically writing to the configuration file is to find the relative position through the comments in the file. Please do not change the comment in the official configuration file, so as not to write the wrong place and cause a program error.

### Create RESTFul API with different route name and controller name

You may use this option frequently. Sometimes, our router will not be associated with the location where the Controllers are stored, then it is very suitable to use this option.

```
php spark create:controller [controller_name] -rest -d -space
```

The "-d" option allows you to customize the router, and "-space" allows you to customize the controller's namespace. We believe that this requirement needs to be met using these two options, so please use this example to demo.

![](https://i.imgur.com/v9wi0WX.png)

The “-d” option can be declared after other options.

Now, in “app/Controllers/System/Api” You can see the new RESTFul Controller File like this :

```php

respond([
"status" => true,
"msg" => "index method successful."
]);
}

/*********/

```

Then, go to "app / Config / Routes.php" and you will see that the routing settings for this Controller have been registered in your configuration file :

```php
/**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

//CliCreate-add-in-2020-04-10 05:49:09
$routes->resource('api/user',[
'controller' =>'\App\Controllers\System\Api\User',
]);

```

### Create "WebSafe" RESTFul API

codeigniter allows you to add the "web safe" attribute when setting routes to have it generate update and delete methods that work with HTML forms

```
php spark create:controller [controller_name] -rest -w
```

The “-w” option can be declared after other options.

![](https://i.imgur.com/ae0gkXi.png)

Then, go to "app / Config / Routes.php" and you will see the websafe attribute appear in the router's settings.

```php
//CliCreate-add-in-2020-04-10 05:54:38
$routes->resource('user',[
'controller' =>'\App\Controllers\User',
'websafe' => 1,
]);
```

### Create RESTFul API and limit the routes made

You can restrict the routes generated with the option. Only routes that match one of these methods will be created. The rest will be ignored.

```
php spark create:controller [controller_name] -rest -o
```

![](https://i.imgur.com/NlzcKem.png)

The “-o” option can be declared after other options.

Now, in “app/Controllers/System/Api” You can see the new RESTFul Controller File like this :

![](https://i.imgur.com/oReZWr7.png)

Then, go to "app / Config / Routes.php" and you will see that the routes you allow to be created are recorded in the router setting :

```php
/**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

//CliCreate-add-in-2020-04-10 05:59:28
$routes->resource('user',[
'controller' =>'\App\Controllers\User',
'only' => ['index','show','create','update','delete'],
]);

```

## create:controller -model

Create a new model for use with the new controller.

* USE
1. Use the options provided by the "create: model" directive.
```
$ php spark create:controller [controller_name] [controller_options] -model [model_name]
```
3. The options provided using the "Create: Model" command are not used.
```
$ php spark create:controller [controller_name] [controller_options] -model=[model_options] [model_name] [entity_name]
```

* Description:
```
Create new controller and model.
```
* Arguments:
1. controller_name : controller name
2. controller_options : Options provid by the controller.
3. model_options : Options provid by the [create:model](#create:model) .If you want to use more than one option, please note that they must be separated by a comma, with "-" removed, and immediately after "=".
1. model_name : model name.
2. entity_name : entity name.If you use the entity option, you can type this argument.

### Create new controller and model

Cli-Create provides a way for you to create a controller and a model at the same time. You can accomplish this with a single line of "spark" command.

```
php spark create:controller [controller_name] -model [model_name]
```

![](https://i.imgur.com/u9jhIon.png)

Now, open app / Controllers / Blog.php and you should see the new controller file, and the correct model namespace has been written:

```php
The options you use must be immediately after "=". You can refer to all the options mentioned in [create:model](#create:model) for these options.

Now, open app / Controllers / Blog.php and you should see the new controller file, and the required model has been declared:

```php
respond([
"status" => true,
"msg" => "index method successful."
]);
}
/********/
```

## create:model

Create a new model file.

* Use
```
$ php spark create:model [model_name] [entity_name] [Options]
```

* Description:
```
Create a new model file.
```
* Arguments:
1. model_name : The model name
2. entity_name : The entity name. If you selected -entity option. You can type this arguments.
* Options:
```
-basic Creates a basic model file.
-entity Uses Entity Classes.
-manual Creates a Manual Model.
-space Creates folders and files according to the path you typed.
```

### Create a normal model

The model class has a few configuration options that can be set to allow the class’ methods to work seamlessly for you.

You can use :

```
$ php spark create:model [model_name]
```

Now, in "app/Models" You can see the new Model File like this :

```php
db =& $db;
}
}
```

### Create a entity model

CodeIgniter supports Entity classes as a first-class citizen in it’s database layer, while keeping them completely optional to use. They are commonly used as part of the Repository pattern, but can be used directly with the Model if that fits your needs better.

You can use this command :

```
$ php spark create:model [model_name] [entity_name] -entity
```
Now, in “app/Models” You can see the new Manual Model File like this :

```php
The namespace in Codeigniter usually maps the actual file storage path, so using this command will automatically create folders according to the value you entered.

Now, in “app/Models/Api/System” You can see the new Manual Model File like this :

```php