June 2019
🔨 Laravel - Api. Inpired by the tutorial from Medium. The goal of the exercise is to create a database managed by tinker or a web client and make some unit testing. You can test the web client version on Heroku
[x] Routes (API)
[x] Database model, table and controller (CRUD)
[x] Model Factories
[x] Seeders
[x] Eloquent ORM & Tinker
[x] Unit testing (PHP Unit)
- CRUD using Tinker
- Unit testing
- Web interface with CRUD
You will need a server to run the application and PHP. For instance Lamp. You also will need composer if you want to add dependencies.
You will need to add an env file on the root using the .env.example as exemple.
In env file change this line:
DB_CONNECTION=mysql
by DB_CONNECTION=sqlite
And delete:
DB_HOST=xxx
DB_PORT=xxx
DB_DATABASE=xxx
DB_USERNAME=xxx
DB_PASSWORD=xxx
To launch the server, go on the directory of this app then launch:
$ php artisan serve
To test the app, you can use the Artisan Tinker command. This allows you to directly view and manipulate your database.
$ php artisan db:seed
$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.1.7 — cli) by Justin Hileman
Create
$p = new App\Task;
$p->title = '<New title>';
$p->description = '<New descritpion>';
$p->save();
Read
$p = \App\Task::all();
or
$p = \App\Task::findorFail(<id>);
Update
$p = \App\Task::findorFail(<id>);
$p->title = '<Updated title>';
$p->description = '<Updated description>';
$p->save();
Delete
$p = \App\Task::findorFail(<id>);
$p ->delete();
or
$p ->forceDelete();
Create a test 'UserTest' in the Feature directory...
$ php artisan make:test UserTest
Create a test 'UserTest' in the Unit directory...
$ php artisan make:test UserTest --unit
In this app, the test has already been created: tests/Feature/TaskTest.php
Launch a test:
$ vendor/bin/phpunit
Create a new app on Keroku.
Link your app to the git repo (production branch), so you could synchronise your repo with Heroku
On Heroku, in settings/ Config Vars, add the env variables (don't upload your .env file!!!):
APP_NAME=Laravel
APP_ENV=local
APP_KEY=<your key>
APP_DEBUG=true
APP_URL=https://<appname>.herokuapp.com/
DB_DATABASE = /app/database/database.sqlite
Create Procfile on root, and add:
web: vendor/bin/heroku-php-apache2 public/
In the .gitignore file in /database/ remove database.sqlite, so you could upload your DB (Heroku will try to find it in /app/database/database.sqlite)
In .htaccess in root, add:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
In this file you will need:
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
and:
"require": {
"ext-pdo_sqlite": "*"
},
Then run:
$ composer update
Of course you also can use the client interface:
$ php artisan serve
And open: http://127.0.0.1:8000/tasks
You can also test the app on Heroku
Use Resource Controller, Artisan and Tinker to set up REST API in Laravel
Getting Started with PHPUnit in Laravel
How to test specific test class using phpunit in laravel
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple back-ends for session and cache storage.
- Expressive, intuitive database ORM.
- Database agnostic schema migrations.
- Robust background job processing.
- Real-time event broadcasting.
Laravel is accessible, powerful, and provides tools required for large, robust applications.
Laravel has the most extensive and thorough documentation and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
If you don't feel like reading, Laracasts can help. Laracasts contains over 1400 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel Patreon page.
- Vehikl
- Tighten Co.
- Kirschbaum Development Group
- 64 Robots
- Cubet Techno Labs
- Cyber-Duck
- British Software Development
- Webdock, Fast VPS Hosting
- DevSquad
- UserInsights
- Fragrantica
- SOFTonSOFA
- User10
- Soumettre.fr
- CodeBrisk
- 1Forge
- TECPRESSO
- Runtime Converter
- WebL'Agence
- Invoice Ninja
- iMi digital
- Earthlink
- Steadfast Collective
- We Are The Robots Inc.
- Understand.io
- Abdel Elrafa
- Hyper Host
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.
The Laravel framework is open-source software licensed under the MIT license.