Home > Article > PHP Framework > How to use laravel to build a backend system
Laravel is a popular PHP web framework that is widely used for the development of web applications and APIs. It is known to provide an easy-to-use, elegant and flexible syntax that allows developers to quickly build complex web applications. In this article, we will discuss how to easily build a simple yet powerful backend system using the Laravel framework.
Step One: Install Laravel
In order for the Laravel framework to develop related functions, we need to install Laravel first. We can use Composer to create a new Laravel application. The method is as follows:
Step1: First install the latest version of Composer. The official website provides download methods for multiple platforms.
Step2: Run the following command:
composer create-project --prefer-dist laravel/laravel laravel-admin
This will create a new Laravel application and install it in a directory called laravel-admin folder. Waiting for the command execution to complete, we can enter this folder to continue the next step.
Step 2: Install Laravel-admin
In order to make the background management more convenient, we can use Laravel-admin. This is a backend management panel for Laravel, which can easily generate CRUD (create, read, update, delete) and some common operations, such as file upload, date and time picker, rich text editor, etc.
We can use Composer to install Laravel-admin as follows:
Step1: Open the terminal and switch to our Laravel project folder, and then enter in the command line:
composer require encore/laravel-admin
Step2: After installation, we need to add service provider for Laravel-admin in our configuration file. Edit the app.php file and add the following code to the providers array:
Encore\Admin\AdminServiceProvider::class,
Step3: Then, we need to prepare the database required by the backend. Run the following command:
php artisan admin:install
This will create a default user and role, which we can then use to log into the backend system and manage our application .
Step 3: Set up routing and controllers
We need to define some routes to connect our backend interface and controller. Edit the routes/admin.php file and add the following routes:
use Encore\Admin\Facades\Admin;
Route::get('/ ', function () {
return Admin::content('Dashboard');
});
This defines a basic route that will specify the content of our homepage. Now we need to create a controller to display our view.
Step1: We can enter the following command in the terminal to create a controller:
php artisan make:controller Admin/DashboardController
Step2: Next, open our DashboardController.php file and add the following content:
namespace App\Http\Controllers\Admin;
use Encore\Admin\Controllers\AdminController;
class DashboardController extends AdminController
{
public function index() { return $this->content('Dashboard'); }</p> <p>}</p> <p>Step3: Finally, we also need to define our new controller route in the routes/admin.php file :</p> <p>Route::get('/', 'Admin\DashboardController@index');</p> <p>Now when accessing our application in the browser, we can see the background instrumentation the contents of the disk. </p> <p>Step 4: Build the backend interface<br>Now that we have defined our routes and controllers, the next step is to build our backend interface. We can use the templates and components provided by Laravel-admin to quickly build our backend system. </p> <p>Step1: Open the app/Admin/bootstrap.php file and add the following code: </p> <p>Admin::js(asset('vendor/laravel-admin/AdminLTE/bootstrap/js/bootstrap. min.js'));<br>Admin::js(asset('vendor/laravel-admin/AdminLTE/dist/js/app.min.js'));<br>Admin::js(asset(' vendor/laravel-admin/AdminLTE/plugins/datatables/jquery.dataTables.min.js'));<br>Admin::js(asset('vendor/laravel-admin/AdminLTE/plugins/datatables/dataTables.bootstrap. min.js'));<br>Admin::js(asset('vendor/laravel-admin/AdminLTE/plugins/slimScroll/jquery.slimscroll.min.js'));<br>Admin::js(asset ('vendor/laravel-admin/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js'));<br>Admin::js(asset('vendor/laravel-admin/AdminLTE/plugins/ select2/select2.full.min.js'));<br>Admin::js(asset('vendor/laravel-admin/AdminLTE/plugins/ckeditor/ckeditor.js'));</p> <p>Admin ::css(asset('vendor/laravel-admin/AdminLTE/bootstrap/css/bootstrap.min.css'));<br>Admin::css(asset('vendor/laravel-admin/AdminLTE/dist/css /AdminLTE.min.css'));<br>Admin::css(asset('vendor/laravel-admin/AdminLTE/dist/css/skins/skin-blue.min.css'));<br>Admin ::css(asset('vendor/laravel-admin/AdminLTE/plugins/datatables/dataTables.bootstrap.min.css'));<br>Admin::css(asset('vendor/laravel-admin/eonasdan-bootstrap -datetimepicker/build/css/bootstrap-datetimepicker.min.css'));<br>Admin::css(asset('vendor/laravel-admin/AdminLTE/plugins/select2/select2.min.css')); </p> <p>这将添加一些必要的CSS和JS文件,以便后台模板能够正确地运行。现在我们可以创建我们的后台模板。</p> <p>Step2: 找到resources/views/vendor/admin/layout.blade.php文件并添加以下代码:</p> <p><!DOCTYPE html><br><html><br><head></p> <pre class="brush:php;toolbar:false"><meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>@yield('title') - {{ admin_title() }}</title> {!! admin_css() !!} {!! admin_js() !!}
<div class="wrapper"> {!! $content !!} </div>