phần 1:
- Truy cập trang chủ laravel : trang chủ laravel
Kéo xuống phần Installation Via Composer xem cách tạo một dự án mới.
Tạo một dự án mới với tên api_backend bằng lệnh sau:
composer create-project laravel/laravel api_backend
Vào api.php:
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('product','ProductControlle@index');
- Vào ProductController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductControlle extends Controller
{
public function index()
{
dd(123);
}
}
Vào terminal chạy lệnh:
php artisan make:controller ProductControlle
- vào navicat tạo databasse tên api_backend.
- vào terminal chạy server dự án lên.
- Vào terminal chạy lệnh: php artisan make:model Product -m để tạo model Product.
- vào file 2021_11_15_074953_create_products_table.php :
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
- Vào terminal chạy lệnh: php artisan migrate
Nếu bị lỗi vào file AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
check navicat:
Thêm một vài dữ liệu cho bảng product:
- Vào ProductController.php:<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductControlle extends Controller
{
public function index()
{
$product = Product::all();
return response()->json([
'code'=> 200,
'data'=>$product
],200);
}
}
Chạy lại api xem kết quả:
phần 2
- Tạo một dự án frontend mới :
composer create-project laravel/laravel api_frontend
Chạy dự án lên với port 8080:
php artisan serve --port=8080
Vào terminal chạy lệnh : php artisan make:controller ProductController
- Vào web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('product','ProductController@index');
Vào ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index(){
$products = json_decode(file_get_contents('http://127.0.0.1:8000/api/product'));
echo '<pre>';
print_r($products);
}
}
lên web phía frontend xem :
- Vào ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index(){
$products = json_decode(file_get_contents('http://127.0.0.1:8000/api/product'));
$products = $products->data;
return view('products.index',compact('products'));
}
}
Và bây giờ việc của các bạn là tạo mới một view index để hiển thị sản phẩm ra màn hình nữa thôi.
có thể tham khỏa ở khóa larave-framework mình đã viết trên blog.
Tham khảo:
Có thể sử dụng kết hợp với Guzzle
link video hướng dẫn: liên kết
0 Nhận xét