Skip to content

Becode - Laravel - CRUD using Tinker and SQLite / Unit testing / Web interface with CRUD - (Demo on Heroku)

License

Notifications You must be signed in to change notification settings

Raigyo/laravel-api

Repository files navigation

Becode - Build an API with Laravel

Becode logo

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)

Features

  • CRUD using Tinker
  • Unit testing
  • Web interface with CRUD

Installation - Backend

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.

Seed the database

$ php artisan db:seed

Use Tinker

$ 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();

Use PHP Unit

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

Deployment on Heroku (it's the version on the branch production in this repo)

Heroku app

Create a new app on Keroku.

Link your app to the git repo (production branch), so you could synchronise your repo with Heroku

Env variables

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

Procfile

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)

.htaccess

In .htaccess in root, add:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

composer.json

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

Frontend

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

Useful links

Build an API with Laravel

Eloquent ORM

Tinker: Laravel doc

Use Resource Controller, Artisan and Tinker to set up REST API in Laravel

Testing: Laravel doc

Getting Started with PHPUnit in Laravel

How to test specific test class using phpunit in laravel

Build Status Total Downloads Latest Stable Version License

About 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:

Laravel is accessible, powerful, and provides tools required for large, robust applications.

Learning Laravel

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.

Laravel Sponsors

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.

Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the Laravel documentation.

Security Vulnerabilities

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.

License

The Laravel framework is open-source software licensed under the MIT license.

About

Becode - Laravel - CRUD using Tinker and SQLite / Unit testing / Web interface with CRUD - (Demo on Heroku)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published