Laravel website 04-create Background Article management, laravel04 --
Route Configuration
Route: group (['ddleware '=> 'auth', 'namespace' => 'admin', 'prefix' => 'admin'], function (){
Route: get ('/', 'homecontroller @ Index'); // match the URL Route: resource ('Article ', 'articlecontroller'); // match the URL containing "/admin/article"
Route: post ('/upload', 'uploadcontroller @ Index'); // match the URL containing "/admin/upload"
});
Route GroupTo allow shared routing attributes, such as middleware and namespace, we do not need to set a single common attribute for each Route. The common attributes are placed in the Route as Arrays :: in the first parameter of the group method.
Middleware('Middleware '=> 'auth') to verify whether the user is logged on.
Namespace('Namespace '=> 'admin'), which specifies the PHP namespaces common to all controllers in the group.
Route prefix('Prefix' => 'admin'), you can add the specified URI prefix to the route in each routing group. Here, we add the routing prefix admin to all URIs in the routing group.
Resource Routing(Route: resource), which can match a group of URIs. The matching details are as follows.
Action |
URI |
Operation |
Route name |
GET |
/article |
Index |
Article. index |
GET |
/article/create |
Create |
Article. create |
POST |
/article |
Store |
Article. store |
GET |
/article/{article} |
Show |
Article. show |
GET |
/article/{article}/edit |
Edit |
Article. edit |
PUT/PATCH |
/article/{article} |
Update |
Article. update |
DELETE |
/article/{article} |
Destroy |
Article. destroy |
Controller
php artisan make:controller Admin/HomeControllerphp artisan make:controller Admin/UploadControllerphp artisan make:controller Admin/ArticleController
Run the preceding three commands to create the controller Used in the background. The first is the background homepage, the second is the processing of uploads, and the last is the management of articles.
In ArticleController, you need to establish methods that match resource routes to implement various resource routing functions.
public function index(){ $articles = DB::table('articles') ->select('id', 'body', 'title') ->orderBy('id', 'desc') ->paginate(5); return view('admin/article/index', ['articles' => $articles]);}
The above is the article list function, where table names, columns, sorting, and pages are specified. Then the retrieved data is displayed in the view.
Qiniu is integrated here to implement the cloud upload function.
Run the following command to install the SDK for qiniu PHP
composer require qiniu/php-sdk
Write the key applied from qiniu in. env, and then the env function can be used in the controller to read the key. This ensures that the released version does not disclose its important information.
View
Create the admin directory under the resources/views directory and create home. blade. php to display the background homepage. Create the article subdirectory under the admin directory, and create the create. blade. php, edit. blade. php, and index. blade. php files. Used to create, edit, and list Articles.
Key points:
- There are two ways to display the text at the front end :{!! $ Article-> body !!} And {$ article-> body}. The HTML in the front can be output as is, while the HTML in the back will be escaped to output the HTML code in the browser foreground, instead of letting the browser parse HTML.
- Ajax submit Image Upload, be sure to append the X-CSRF-TOKEN in the header, otherwise the background if not received, will report an error. Reference: 'x-CSRF-TOKEN ':' {csrf_token ()}}'
I have selected some key points of each link and wrote them in my blog. The source code can be obtained on my GitHub. Welcome to the discussion.