This is the 27th day of my participation in the August Genwen Challenge.More challenges in August
I. Classification details API
1.1 Classification detail controller method
/** * category details */
public function show(Category $category)
{
return $category;
}
Copy the code
We’re going to use the routing model, the return model.
1.2 Test Effect:
You can see that the current category details data has been returned.
However, it is important to note that when you get the classification details, you need to get the list of all the categories when editing. However, we do not display the disabled, so we need to modify the control method of the list. By default, the list does not contain the disabled, as long as you pass all will get all including the disabled, as follows:
public function index(Request $request)
{
$type = $request->input('type');
if ($type= ='all') {
return cache_category_all();
}
else {
returncache_category(); }}Copy the code
This way we can write parameter descriptions in the document, requiring full passall
, the disabled data cannot be retrieved, which is consistent with the service scenario.
Update classification API
2.1 Update classification control method
/** * Update classification details */
public function update(Request $request, Category $category)
{
$request->validate([
'name'= >'required|max:16'
], [
'name.required'= >'Category name cannot be empty'
]);
/ / get the pid
$pid = $request -> input('pid'.0);
/ / calculate level
$level = $pid= =0 ? 1 : (Category::find($pid) -> level + 1); / / calculate level
// Category does not exceed level 3
if ($level > 3) return $this->response->errorBadRequest('Must not exceed grade 3 classification');
$updateData = [
'name'= >$request -> input('name'),
'pid'= >$pid.'level'= >$level,];$category->update($updateData);
// Successful update requires a cache refresh
forget_cache_category_all();
return $this->response->noContent();
}
Copy the code
2.2 Test Effect
2.3 optimization
We can see that there is code redundancy in validation for additions and updates, so we write a function in the current category controller to determine form validation and calculation:
/** * Verify the submitted arguments */
protected function checkInput($request) {
$request->validate([
'name'= >'required|max:16'
], [
'name.required'= >'Category name cannot be empty'
]);
/ / get the pid
$pid = $request -> input('pid'.0);
/ / calculate level
$level = $pid= =0 ? 1 : (Category::find($pid) -> level + 1); / / calculate level
// Category does not exceed level 3
if ($level > 3) return $this->response->errorBadRequest('Must not exceed grade 3 classification');
return [
'name'= >$request -> input('name'),
'pid'= >$pid.'level'= >$level,]; }Copy the code
Modify add and update methods:
/** * Add category */
public function store(Request $request)
{
$insertData = $this->checkInput($request);
if(! is_array($insertData)) return $insertData;
Category::create($insertData);
// The cache needs to be refreshed
forget_cache_category_all();
return $this->response->created();
}
Copy the code
/** * Update classification details */
public function update(Request $request, Category $category)
{
$updateData = $this->checkInput($request);
if(! is_array($updateData)) return $updateData;
$category->update($updateData);
// Successful update requires a cache refresh
forget_cache_category_all();
return $this->response->noContent();
}
Copy the code
The tests found the same effect, but the code had been streamlined. You can see how easy and elegant laravel is for us.
Disable and enable the classification API
3.1 Defining whether to enable and disable classified routing
// Disable and enable routing
$api->patch('category/{category}/status', [CategoryController::class, 'status']);
Copy the code
3.2 Controller Method
/** * state disables and enables */
public function status(Category $category)
{
$category->status = $category -> status == 1 ? 0 : 1;
$category->save();
// Update requires a cache refresh
forget_cache_category_all();
return $this->response()->noContent();
}
Copy the code
3.3 Test Effect
3.4 Optimization (Observer)
As you can see, every time the data changes, we have to clear the cache, but now we’re writing it directly in the method, so we can write it in the model event instead of having to call the controller method every time. But if we write it in the model, the code in the model is bloated, so we use observers. Create observer command:php artisan make:observer CategoryObserver --model=Category
inCategoryObserver.php
Observer write events clear cache:
Then deleteCategoryController.php
Delete clear cache code in controller:
And then inapp/Providers/AppServiceProvider.php
To register our observers:
1 test
You can see that the added items are now disabled. Let’s get the list of categories:You can see that there are added items that have not been disabled. We turn the added item into enabled state by disabling and enabling the interface call, and then retrieve it to see if it’s gone from the cache or fetched from the database again. Effect: You can see that the effect is the same, but we use an observer to listen for changes in the model.
If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.