Skip to content

Commit cc5efea

Browse files
committed
Initial commit
0 parents  commit cc5efea

File tree

108 files changed

+12741
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+12741
-0
lines changed

.editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2
16+
17+
[docker-compose.yml]
18+
indent_size = 4

.env.example

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
APP_NAME=Laravel
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_URL=http://localhost
6+
7+
LOG_CHANNEL=stack
8+
LOG_DEPRECATIONS_CHANNEL=null
9+
LOG_LEVEL=debug
10+
11+
DB_CONNECTION=mysql
12+
DB_HOST=127.0.0.1
13+
DB_PORT=3306
14+
DB_DATABASE=laravel_restapi
15+
DB_USERNAME=root
16+
DB_PASSWORD=
17+
18+
BROADCAST_DRIVER=log
19+
CACHE_DRIVER=file
20+
FILESYSTEM_DISK=local
21+
QUEUE_CONNECTION=sync
22+
SESSION_DRIVER=file
23+
SESSION_LIFETIME=120
24+
25+
MEMCACHED_HOST=127.0.0.1
26+
27+
REDIS_HOST=127.0.0.1
28+
REDIS_PASSWORD=null
29+
REDIS_PORT=6379
30+
31+
MAIL_MAILER=smtp
32+
MAIL_HOST=mailhog
33+
MAIL_PORT=1025
34+
MAIL_USERNAME=null
35+
MAIL_PASSWORD=null
36+
MAIL_ENCRYPTION=null
37+
MAIL_FROM_ADDRESS="[email protected]"
38+
MAIL_FROM_NAME="${APP_NAME}"
39+
40+
AWS_ACCESS_KEY_ID=
41+
AWS_SECRET_ACCESS_KEY=
42+
AWS_DEFAULT_REGION=us-east-1
43+
AWS_BUCKET=
44+
AWS_USE_PATH_STYLE_ENDPOINT=false
45+
46+
PUSHER_APP_ID=
47+
PUSHER_APP_KEY=
48+
PUSHER_APP_SECRET=
49+
PUSHER_HOST=
50+
PUSHER_PORT=443
51+
PUSHER_SCHEME=https
52+
PUSHER_APP_CLUSTER=mt1
53+
54+
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
55+
VITE_PUSHER_HOST="${PUSHER_HOST}"
56+
VITE_PUSHER_PORT="${PUSHER_PORT}"
57+
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
58+
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

.gitattributes

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
*.blade.php diff=html
4+
*.css diff=css
5+
*.html diff=html
6+
*.md diff=markdown
7+
*.php diff=php
8+
9+
/.github export-ignore
10+
CHANGELOG.md export-ignore
11+
.styleci.yml export-ignore

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/node_modules
2+
/public/build
3+
/public/hot
4+
/public/storage
5+
/storage/*.key
6+
/vendor
7+
.env
8+
.env.backup
9+
.phpunit.result.cache
10+
Homestead.json
11+
Homestead.yaml
12+
auth.json
13+
npm-debug.log
14+
yarn-error.log
15+
/.idea
16+
/.vscode

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
# Laravel REST API
3+
4+
This is a basic CRUD API made with the latest version of Laravel (9.x), a popular framework made on top of PHP. The project makes use of Service Layer-Repository Pattern in order to keep the code clean and organized, custom authentication, and authorization with the help of Spatie's permission package.
5+
6+
## Basic Usage
7+
8+
Follow the steps below in order to start using this application.
9+
10+
#### Create Database
11+
12+
`$ php artisan db:create`
13+
14+
#### Define database schema
15+
16+
`$ php artisan migrate`
17+
18+
#### Populate the database with data
19+
20+
`$php artisan db:seed`
21+
22+
#### Run the application
23+
24+
`php artisan serve`
25+
26+
#### Authentication credentials
27+
28+
29+
Password: `password`
30+
31+
## Test Endpoints
32+
33+
Use the following endpoints in order to test the application.
34+
35+
#### Authentication
36+
37+
User Register: **POST** `localhost:8000/api/register`\
38+
User Login: **POST** `localhost:8000/api/login`\
39+
User Logout: **POST** `localhost:8000/api/login`
40+
41+
#### Authorization
42+
43+
List Roles: **GET** `localhost:8000/api/roles`\
44+
Get Role: **GET** `localhost:8000/api/roles/3`\
45+
Add Role: **POST** `localhost:8000/api/roles`\
46+
Update Role: **PUT** `localhost:8000/api/roles/3`\
47+
Delete Role: **DELETE** `localhost:8000/api/roles/3`\
48+
Search Keyword: **GET** `localhost:8000/api/roles?search=lorem`
49+
50+
#### Articles
51+
52+
List Articles: **GET** `localhost:8000/api/articles`\
53+
Get Article: **GET** `localhost:8000/api/articles/3`\
54+
Add Article: **POST** `localhost:8000/api/articles`\
55+
Update Article: **PUT** `localhost:8000/api/articles/3`\
56+
Delete Article: **DELETE** `localhost:8000/api/articles/3`\
57+
Search Keyword: **GET** `localhost:8000/api/articles?search=lorem`
58+
59+
#### Users
60+
61+
List Users: **GET** `localhost:8000/api/users`\
62+
Get User: **GET** `localhost:8000/api/users/3`\
63+
Update User: **PUT** `localhost:8000/api/users/3`\
64+
Delete User: **DELETE** `localhost:8000/api/users/3`\
65+
Search Keyword: **GET** `localhost:8000/api/users?search=lorem`
66+
67+
**Node:** If you're using Postman to test this API, don't forget to add HTTP Header key "Accept" with the value "application/json".

app/Console/Commands/dbcreate.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\DB;
7+
8+
class dbcreate extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'db:create {name?}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new MySQL database based on the database config file or the provided name.';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
/**
35+
* Execute the console command.
36+
*
37+
* @return int
38+
*/
39+
public function handle()
40+
{
41+
$schemaName = $this->argument('name') ?: config("database.connections.mysql.database");
42+
$charset = config("database.connections.mysql.charset",'utf8mb4');
43+
$collation = config("database.connections.mysql.collation",'utf8mb4_unicode_ci');
44+
45+
config(["database.connections.mysql.database" => null]);
46+
47+
$query = "CREATE DATABASE IF NOT EXISTS $schemaName CHARACTER SET $charset COLLATE $collation;";
48+
49+
DB::statement($query);
50+
51+
config(["database.connections.mysql.database" => $schemaName]);
52+
}
53+
}

app/Console/Kernel.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Console;
4+
5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
7+
8+
class Kernel extends ConsoleKernel
9+
{
10+
/**
11+
* Define the application's command schedule.
12+
*
13+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
14+
* @return void
15+
*/
16+
protected function schedule(Schedule $schedule)
17+
{
18+
// $schedule->command('inspire')->hourly();
19+
}
20+
21+
/**
22+
* Register the commands for the application.
23+
*
24+
* @return void
25+
*/
26+
protected function commands()
27+
{
28+
$this->load(__DIR__.'/Commands');
29+
30+
require base_path('routes/console.php');
31+
}
32+
}

app/Exceptions/Handler.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Exceptions;
4+
5+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6+
use Throwable;
7+
8+
class Handler extends ExceptionHandler
9+
{
10+
/**
11+
* A list of exception types with their corresponding custom log levels.
12+
*
13+
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
14+
*/
15+
protected $levels = [
16+
//
17+
];
18+
19+
/**
20+
* A list of the exception types that are not reported.
21+
*
22+
* @var array<int, class-string<\Throwable>>
23+
*/
24+
protected $dontReport = [
25+
//
26+
];
27+
28+
/**
29+
* A list of the inputs that are never flashed to the session on validation exceptions.
30+
*
31+
* @var array<int, string>
32+
*/
33+
protected $dontFlash = [
34+
'current_password',
35+
'password',
36+
'password_confirmation',
37+
];
38+
39+
/**
40+
* Register the exception handling callbacks for the application.
41+
*
42+
* @return void
43+
*/
44+
public function register()
45+
{
46+
$this->reportable(function (Throwable $e) {
47+
//
48+
});
49+
}
50+
}

app/Filters/Search.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Filters;
4+
use Samushi\QueryFilter\Filter;
5+
6+
class Search extends Filter
7+
{
8+
private $search;
9+
10+
public function __construct(array $search = ['query'])
11+
{
12+
$this->search = $search;
13+
}
14+
15+
/**
16+
* Search Result by whereLike
17+
* @param $builder
18+
* @return mixed
19+
*/
20+
protected function applyFilter($builder)
21+
{
22+
return $builder->whereLike($this->search, request()->get($this->fillterName()));
23+
}
24+
}

0 commit comments

Comments
 (0)