Download the PHP package alirah/laravel-rest without Composer
On this page you can find all versions of the php package alirah/laravel-rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alirah/laravel-rest
More information about alirah/laravel-rest
Files in alirah/laravel-rest
Package laravel-rest
Short Description This package used for building Laravel RESTFUL API much easier.
License MIT
Informations about the package laravel-rest
Laravel Rest
This is a Laravel Package to build RESTFUL API much easier. This package supports Laravel 5.8+
.
With this package you can build all the things needed for RESTFUL API with only one command. Such as Controller, Resource, Request, Model, Migration, Seeder, Factory, Route, Test and swagger. Moreover, you can delete it as well.
List of contents
- Laravel Rest
- List of contents
- Install
- Configure
- Swagger
- Testing
- How to use
- Create
- Delete
- Versioning
- Versioning Create
- Versioning Delete
- Rest Facade
- Example
- Security
- Credits
- License
Install
Via Composer
Configure
If you are using Laravel 5.5
or higher than you don't need to add the provider and alias. (Skip to b)
a. In your config/app.php
file add these two lines.
b. then run the command in below to publish config/laravel-rest.php
file in your config directory :
After running the command , you can set your desired configuration.
Swagger
In order to use Swagger, you need to follow these steps:
a. Run this command:
b. Next, publish config/views from Service Provider:
c. Copy the code in below then paste it to the top of the Controller class in App\Http\Controller\Controller :
At the end, your controller must be like this:
d. Turn swagger field to true in your config\laravel-rest.php:
e. To generate your swagger run:
By default, the swagger route is 'api/documentation' but you can change it in config/l5-swagger.php. To see the full documentation you can check https://github.com/DarkaOnLine/L5-Swagger.
Testing
To get better results we suggest to use sqlite database for your tests. To use sqlite follow these steps:
a. First uncomment the DB_CONNECTION line in phpunit.xml in the root folder
b. Change the sqlite database path in config/database.php:
c. Create a file database.sqlite in database folder
d. To run your tests run command:
How to use
Create
To create a rest recourse, you should run:
This Command create these files:
- app\Models\ModelName.php
- app\Controllers\ModelName\ModelNameController.php
- app\Request\ModelName\StoreRequest.php
- app\Request\ModelName\UpdateRequest.php
- app\Resource\ModelName\ModelNameResource.php
- database\migrations\...model_names.php
- database\factories\ModelNameFactory.php
- database\seeders\ModelNameSeeder.php
- tests\Feature\ModelName\ModelNameTest.php
-
and add the line below in the end of ./routes/api.php
- You can change the api.php file in config.
- For overriding a file with same name, it needs your permission in cmd.
- You can use -F or --force flag to force it.
Delete
To delete a rest recourse, you should run:
This Command delete these files:
- app\Models\ModelName.php
- app\Controllers\ModelName\ModelNameController.php
- app\Request\ModelName\StoreRequest.php
- app\Request\ModelName\UpdateRequest.php
- app\Resource\ModelName\ModelNameResource.php
- database\migrations\...model_names.php
- database\factories\ModelNameFactory.php
- database\seeders\ModelNameSeeder.php
- tests\Feature\ModelName\ModelNameTest.php
-
and remove the line below at the end of ./routes/api.php
- You can change the api.php file in config. For deleting every file, it needs your permission in cmd.
- You can use -F or --force flag to force it.
Versioning
For versioning you can put prefix for your swagger routes in your config:
Versioning Create
TO create version resource You can run:
This Command create these files:
- app\Models\ModelName.php
- app\Controllers\V1\ModelName\ModelNameController.php
- app\Request\V1\ModelName\StoreRequest.php
- app\Request\V1\ModelName\UpdateRequest.php
- app\Resource\V1\ModelName\ModelNameResource.php
- database\migrations\...model_names.php
- database\factories\ModelNameFactory.php
- database\seeders\ModelNameSeeder.php
- tests\Feature\V1\ModelName\ModelNameTest.php
Versioning Delete
To delete a version resource You can run:
This Command delete these files:
- app\Models\ModelName.php
- app\Controllers\V1\ModelName\ModelNameController.php
- app\Request\V1\ModelName\StoreRequest.php
- app\Request\V1\ModelName\UpdateRequest.php
- app\Resource\V1\ModelName\ModelNameResource.php
- database\migrations\...model_names.php
- database\factories\ModelNameFactory.php
- database\seeders\ModelNameSeeder.php
- tests\Feature\V1\ModelName\ModelNameTest.php
Rest Facade
The Rest facade is used for return JsonResource in controller.
Available methods for Rest facade:
ok($data)
: return $data with 200 status codeaccepted($data)
: return $data with 202 status codebadRequest($data)
: return $data with 400 status codeunauthorized($data)
: return $data with 401 status codeforbidden($data)
: return $data with 403 status codenotFound($data)
: return $data with 404 status codeerror($data)
: return $data with 500 status codecustom($data, $statusCode)
: return $data with $statusCode status code
The $data should be Laravel resource or an array
Example
When enable swagger and run this is the result:
- After fill all the required(TODO) fields you can run for testing and for generating swagger
- If you have not changed the default route in config/l5-swagger.php, you can visit the Swagger output in 'api/documentation'
app\Models\Blog.php
app\Http\Controllers\Blog\BlogController.php
app\Http\Request\Blog\StoreRequest.php
app\Http\Request\Blog\UpdateRequest.php
app\Http\Resource\Blog\BlogResource.php
database\migrations\...create_blogs_table.php
database\factories\BlogFactory.php
database\seeders\BlogSeeder.php
tests\Feature\Blog\BlogTests.php
routes/api.php
Change log
Please see CHANGELOG for more information on what has been changed recently.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.