web và route api thường như sau:
| Route Web | Route API | |
| URL | Đường dẫn thân thiện với Người dùng cuối. Ví dụ:
|
Đường dẫn thường phứt tạp, đặt theo quy cách đánh số phiên bản dành cho Lập trình viên sử dụng. Thường có tiền tố /api ở đầu. Ví dụ:
|
| Xác thực | Sử dụng FORM Đăng nhập thông thường | Sử dụng Token để chứng thực. Thường là JWT Token |
route cho Web và API, trong Laravel đã phân định sẵn 2 file cấu hình dành riêng cho các loại route này:
routes/web.php: là file cấu hình quy định các route dành cho Webroutes/api.php: là file cấu hình quy định các route dành cho API. Khi sử dụng các route này thì mặc định đường dẫn URL sẽ có tiền tố /api/url-route-cua-banphp artisan make:controller Api/ApiController
routes/api.php<?php
use App\Http\Controllers\Api\ApiController;
Route::get('/thongke/top3_sanpham_moinhat', [ApiController::class, 'thongke_top3_sanpham_moinhat'])->name('api.thongke.top3_sanpham_moinhat');
app/Http/Controllers/Api/ApiControllerthongke_top3_sanpham_moinhat()<?php
use DB;
public function thongke_top3_sanpham_moinhat() {
// Sử dụng Query Builder
// Query top 3 loại sản phẩm (có sản phẩm) mới nhất
$ds_top3_newest_loaisanpham = DB::table('cusc_sanpham')
->join('cusc_loai', 'cusc_loai.l_ma', '=', 'cusc_sanpham.l_ma')
->orderBy('l_capNhat')->take(3)->get();
return response()->json(array(
'code' => 200,
'result' => $ds_top3_newest_loaisanpham,
));
}
routes/web.phpRoute::get('/thongke', 'Frontend\FrontendController@thongke')->name('frontend.pages.thongke');
thongke() hiển thị view trong FrontendFrontend/FrontendController.phppublic function thongke() {
// Hiển thị view Thống kê
return view('frontend.pages.thongke');
}
resources/views/frontend/pages/thongke.blade.php{{-- View này sẽ kế thừa giao diện từ `frontend.layouts.master` --}}
@extends('frontend.layouts.master')
{{-- Thay thế nội dung vào Placeholder `title` của view `frontend.layouts.master` --}}
@section('title')
Thống kê số liệu Shop Hoa tươi - Sunshine
@endsection
{{-- Thay thế nội dung vào Placeholder `custom-css` của view `frontend.layouts.master` --}}
@section('custom-css')
<style>
.img-hinhdaidien {
width: 50px;
height: 50px;
}
</style>
@endsection
{{-- Thay thế nội dung vào Placeholder `main-content` của view `frontend.layouts.master` --}}
@section('main-content')
<div class="container">
<h1 class="text-center">Thống kê Số liệu Shop Hoa Tươi</h1>
<div id="thongke_sanpham" ng-controller="thongKeSanPhamController">
<h2>Thống kê về Sản phẩm</h2>
<div class="card card-info">
<div class="card-header">Top 3 sản phẩm mới nhất</div>
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<td>STT</td>
<td>Hình đại diện</td>
<td>Tên sản phẩm</td>
<td>Giá</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="sp in danhsach_top3_sanpham_moinhat">
<td><% $index + 1 %></td>
<td>
<img ng-src="/storage/photos/<% sp.sp_hinh %>" class="img-hinhdaidien"/></td>
<td><% sp.sp_ten %></td>
<td><% sp.sp_giaBan | number:0 %></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
{{-- Thay thế nội dung vào Placeholder `custom-scripts` của view `frontend.layouts.master` --}}
@section('custom-scripts')
<script>
// Khai báo controller `thongKeSanPhamController`
app.controller('thongKeSanPhamController', function($scope, $http) {
// Dữ liệu
$scope.danhsach_top3_sanpham_moinhat = [];
// sử dụng service $http của AngularJS để gởi request GET đến route `api.thongke.top3_sanpham_moinhat`
$http({
url: "{{ route('api.thongke.top3_sanpham_moinhat') }}",
method: "GET"
}).then(function successCallback(response) {
// Nếu gởi request thành công thì chuyển dữ liệu sang cho AngularJS hiển thị
$scope.danhsach_top3_sanpham_moinhat = response.data.result;
console.log(response.data.result);
console.log($scope.danhsach_top3_sanpham_moinhat);
}, function errorCallback(response) {
// Lấy dữ liệu không thành công, thông báo lỗi cho khách hàng biết
swal('Có lỗi trong quá trình lấy dữ liệu!', 'Vui lòng thử lại sau vài phút.', 'error');
console.log(response);
});
});
</script>
@endsection
Cùng nhau học tập, khám phá các kiến thức nền tảng về Lập trình web, mobile, database nhé.
Nền tảng kiến thức - Hành trang tới tương lai hân hạnh phục vụ Quý khách!
Khám phá, trải nghiệm ngay
Vui lòng đăng nhập để gởi bình luận!
Đăng nhậpChưa có bình luận nào!