Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created July 18, 2018 05:00
Show Gist options
  • Save ahgood/8c98da003e1a6eb53157157651e30775 to your computer and use it in GitHub Desktop.
Save ahgood/8c98da003e1a6eb53157157651e30775 to your computer and use it in GitHub Desktop.
Lumen step by step
####################################
Quick start:
####################################
mysql -u root -p -e "create database bookstore";
mysql -u root -p bookstore < bookstore.sql
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`author` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
*Additional, export a table without data:
mysqldump -u root -p --no-data bookstore books > bookstore_books.sql
####################################
Step by step:
####################################
0. Install local web server and composer
XAMPP: https://www.apachefriends.org/download.html
Composer: https://getcomposer.org/doc/00-intro.md
1. Create Lumen app:
composer create-project --prefer-dist laravel/lumen bookstore
OR:
php composer.phar create-project --prefer-dist laravel/lumen bookstore
2. Make Lumen app works as in sub-folder:
Move files in ./public to app's root
Update path: /../bootstrap/app.php to /bootstrap/app.php
3. Create a route:
Edit file ./routes/web.php
Add:
$router->get('api/v1/hello', function () {
return 'Hello World, my app!';
});
4. Enable Eloquent
Edit file bootstrap/app.php
Uncomment: $app->withEloquent();
5. Create database:
mysql -u root -e "create database bookstore";
6. Edit file /.env, and update DB information
DB_DATABASE=mybooks
DB_USERNAME=root
DB_PASSWORD=
7. Create a migration, books is table name and must contain "s" at the end
php artisan make:migration create_books_table
8. Edit file /database/migrations/yyyy_mm_dd_ssssss_create_books_table.php
Add columns:
$table->string('name');
$table->string('author');
$table->string('type');
*More column type: https://laravel.com/docs/5.6/migrations
9. Run migrations:
php artisan migrate
10. Add module, save /app/User.php as /app/Book.php
Change "class User" to "class Book"
Change "protected $fillable" part as following:
protected $fillable = ['name', 'author', 'type'];
11. Insert a record:
Create/Edit file: /app/Http/Controllers/BookController.php
<?php
namespace App\Http\Controllers;
use App\Book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
public function add() {
$book = Book::firstOrCreate([
'name' => 'Momo',
'author' => 'Noname',
'type' => 'NA',
]);
return response()->json($book);
}
}
Edit file ./routes/web.php
Add:
$router->get('api/v1/book/add', 'BookController@add');
*If you want to get info from URL query string:
<?php
namespace App\Http\Controllers;
use App\Book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
public function add(Request $request) {
$name = $request->input('name');
$author = $request->input('author');
$type = $request->input('type');
$book = Book::firstOrCreate([
'name' => $name,
'author' => $author,
'type' => $type,
]);
return response()->json($book);
// return response()->json($book)->withCallback($request->input('callback'));
// return response()->json('OK')->withCallback($request->input('callback'));
}
}
12. Display Edit file: /app/Http/Controllers/BookController.php
Edit:
public function show($id) {
$book = Book::findOrFail($id);
return response()->json($book);
}
Edit file ./routes/web.php
Add:
$router->get('api/v1/book/{id}', 'BookController@show');
*Learn more: https://laravel.com/docs/5.6/eloquent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment