ThinkPHP6 backend management system development: realizing backend functions
Introduction:
With the continuous development of Internet technology and market demand, more and more enterprises and Organizations need an efficient, secure, and flexible backend management system to manage business data and conduct operational management. This article will use the ThinkPHP6 framework to demonstrate through examples how to develop a simple but practical backend management system, including basic functions such as permission control, data addition, deletion, modification and query.
php think module admin
Auth.php
and place it in theapp/admin/middleware
directory.namespace appdminmiddleware; use think acadeSession; class Auth { public function handle($request, Closure $next) { // 判断用户是否登录 if (!Session::get('admin')) { return redirect(url('admin/login/index')); } return $next($request); } }
Then, register the middleware in theapp/admin/middleware.php
file:
return [ 'Auth' => appdminmiddlewareAuth::class, ];
Finally, proceed in the route that requires permission control Definition of middleware, for example:
Route::group('admin', function () { Route::group('user', function () { Route::get('index', 'admin/user/index')->middleware('Auth'); }); });
User management:
First, we need to create a user-managed controllerUser.php
and place it in theapp/admin/controller
directory Down.
namespace appdmincontroller; use thinkController; use appdminmodelUser as UserModel; class User extends Controller { public function index() { $userModel = new UserModel(); $users = $userModel->paginate(10); $this->assign('users', $users); return $this->fetch(); } public function create() { // 处理用户的创建逻辑 } public function edit($id) { // 处理用户的编辑逻辑 } public function delete($id) { // 处理用户的删除逻辑 } }
Then, create a user modelUser.php
and place it in theapp/admin/model
directory.
namespace appdminmodel; use thinkModel; class User extends Model { // 表名 protected $table = 'users'; }
Finally, write the view code for the user list in theapp/admin/view/user/index.html
file.
ID | 用户名 | 邮箱 | 操作 |
---|---|---|---|
{$user.id} | {$user.username} | {$user.email} | 编辑 删除 |
Article management:
Similarly, we can create an article management controllerArticle.php
and place it in theapp/admin/controller
directory .
namespace appdmincontroller; use thinkController; use appdminmodelArticle as ArticleModel; class Article extends Controller { public function index() { $articleModel = new ArticleModel(); $articles = $articleModel->paginate(10); $this->assign('articles', $articles); return $this->fetch(); } public function create() { // 处理文章的创建逻辑 } public function edit($id) { // 处理文章的编辑逻辑 } public function delete($id) { // 处理文章的删除逻辑 } }
Similarly, create an article modelArticle.php
and place it in theapp/admin/model
directory.
namespace appdminmodel; use thinkModel; class Article extends Model { // 表名 protected $table = 'articles'; }
Finally, write the view code for the article list in theapp/admin/view/article/index.html
file, similar to the view code for user management.
Summary:
This article uses the ThinkPHP6 framework to develop a simple backend management system, and implements basic functions such as permission control, data addition, deletion, modification and query. Through this example, I hope readers can understand how to use ThinkPHP6 to quickly build a fully functional backend management system. Of course, in actual development, functions can be further improved and performance optimized to adapt to different business needs.
The above is the detailed content of ThinkPHP6 backend management system development: realizing backend functions. For more information, please follow other related articles on the PHP Chinese website!