search
HomeBackend DevelopmentPHP Tutorial在Laravel5. 中使用 AdminLTE

在Laravel5.* 中使用 AdminLTE

image

在Laravel5.* 中使用 AdminLTE

AdminLTE是一个很棒的单纯的由 HTML 和 CSS 构建的后台模板,在这片文章中,我将讲述如何将 AdminLTE 和 Laravel 优雅的整合在一起,而且我们可以通过 Bower 来及时的更新和管理 AdminLTE。

我们使用的工具

  1. Laravel
  2. AdminLTE 2.3.2
  3. Bower
  4. Composer

下载一个全新的 Laravel

如果不太清楚可以去官方网站查看文档link
在此我们直接使用命令行即可

<code>    composer create-project laravel/laravel myapp --prefer-dist</code>

通过这个命令我们创建了一个全新的名字为 myapp 的Laravel项目,如果你成功的话你可以看到下面的图片
image

通过 Bower 下载 AdminLTE

进入到 myapp/public 文件夹

<code>   cd myapp/public</code>

在这个文件夹下执行下面的命令

<code>    bower install admin-lte </code>

一旦完成,你会发现多了一个 bower_componets 的文件夹,而且在这个文件夹中你会看到 AdminLTE

将 AdminLTE 的starter.html 转化为 Blade 模板

Laravel 在此使用了一个很好的模板引擎 Blade,为了更充分的利用Blade,我们需要将一些常规的通用的 HTML 的 起始页面应用到 Blade 模板中,首先创建一个 view 在 resources/views文件夹中,命名为admin_template.blade.php,而后我们为这个页面创建一个对应的路由。如下面我所创建的

<code>    Route::get(&#39;admin&#39;, function () {        return view(&#39;admin_template&#39;);    });    </code>

然后,将bower_components/admin-lte/starter.html中的内容复制到我们视图模板中,并且将其中的相关链接指向我们的 AdminLTE 的对应目录下
css 和 js 的相关的链接指向相应的目录下,而后我们通过 localhost:8000/admin 查看页面的变化,此时页面变成了如下图:
image

现在我们拥有了所有的使用 AdminLTE 的所有的资源,下面对我们的主要视图增加最后的收尾工作,我将分开这个模板为三个文件,sidebar.blade.php, header.blade.php, 和 footer.blade.php
这三个文件的内容分别是admin_template.blade.phpheader 部分和 aside 部分和footer 部分,将这三部分截取出来依次放到三个文件中。

最后的润色工作

现在我们已经将我们的模板个性化的分离开了,下面我们需要设置我们的最初的admin_template.blade.php
模板以便于内容动态加载,如下所示:

<code>@include('header')<!-- Sidebar -->@include('sidebar')<!-- Content Wrapper. Contains page content --><div class="content-wrapper">    <!-- Content Header (Page header) -->    <section class="content-header">        <h1 id="page-title-or-Page-Title-small-page-description-or-null-small">            {{ $page_title or "Page Title" }}            <small>{{ $page_description or null }}</small>        </h1>        <!-- You can dynamically generate breadcrumbs here -->        <ol class="breadcrumb">            <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li>            <li class="active">Here</li>        </ol>    </section>    <!-- Main content -->    <section class="content">        <!-- Your Page Content Here -->        @yield('content')    </section><!-- /.content -->
</div>
<!-- /.content-wrapper --><!-- Footer -->@include('footer')</code>

在上面代码中,我们添加了contetn,这里包含着我们的主要的内容,增加了页面标题针对不同的页面,将其重命名为dashboard.blade.php现在这个定Blade布局已经可以使用了。

测试页面

为了验证我们之前所做的工作,我将创建一个简单的页面

1.创建 test.blade.php

<code>@extends('dashboard')@section('content')<div class="'row'">    <div class="'col-md-6'">        <!-- Box -->        <div class="box box-primary">            <div class="box-header with-border">                <h3 id="Randomly-Generated-Tasks">Randomly Generated Tasks</h3>                <div class="box-tools pull-right">                    <button class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse"><i class="fa fa-minus"></i></button>                    <button class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove"><i class="fa fa-times"></i></button>                </div>            </div>            <div class="box-body">                @foreach($tasks as $task)                    <h5 id="task-name-small-class-label-label-task-color-pull-right-task-progress-small">                        {{ $task['name'] }}                        <small class="label label-{{$task['color']}} pull-right">{{$task['progress']}}%</small>                    </h5>                    <div class="progress progress-xxs">                        <div class="progress-bar progress-bar-{{$task['color']}}" style="width: {{$task['progress']}}%"></div>                    </div>                @endforeach            </div>
<!-- /.box-body -->            <div class="box-footer">                <form action="'#'">                    <input type="'text'" placeholder="'New" task class="'form-control" input-sm>                </form>            </div>
<!-- /.box-footer-->        </div>
<!-- /.box -->    </div>
<!-- /.col -->    <div class="'col-md-6'">        <!-- Box -->        <div class="box box-primary">            <div class="box-header with-border">                <h3 id="Second-Box">Second Box</h3>                <div class="box-tools pull-right">                    <button class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse"><i class="fa fa-minus"></i></button>                    <button class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove"><i class="fa fa-times"></i></button>                </div>            </div>            <div class="box-body">                A separate section to add any kind of widget. Feel free                to explore all of AdminLTE widgets by visiting the demo page                on <a href="https://almsaeedstudio.com">Almsaeed Studio</a>.            </div>
<!-- /.box-body -->        </div>
<!-- /.box -->    </div>
<!-- /.col -->
</div>
<!-- /.row -->@endsection</code>

2.创建TestController.php

<code>    php artisan make:controller TestController --plain</code>

下面是这个控制器的代码部分:

<code>        <?php            namespace App\Http\Controllers;            use Illuminate\Http\Request;            use App\Http\Requests;            use App\Http\Controllers\Controller;            class TestController extends Controller            {                public function index() {                $data[&#39;tasks&#39;] = [                        [                            &#39;name&#39; => &#39;Design New Dashboard&#39;,                            &#39;progress&#39; => &#39;87&#39;,                            &#39;color&#39; => &#39;danger&#39;                        ],                        [                            &#39;name&#39; => &#39;Create Home Page&#39;,                            &#39;progress&#39; => &#39;76&#39;,                            &#39;color&#39; => &#39;warning&#39;                        ],                        [                            &#39;name&#39; => &#39;Some Other Task&#39;,                            &#39;progress&#39; => &#39;32&#39;,                            &#39;color&#39; => &#39;success&#39;                        ],                        [                            &#39;name&#39; => &#39;Start Building Website&#39;,                            &#39;progress&#39; => &#39;56&#39;,                            &#39;color&#39; => &#39;info&#39;                        ],                        [                            &#39;name&#39; => &#39;Develop an Awesome Algorithm&#39;,                            &#39;progress&#39; => &#39;10&#39;,                            &#39;color&#39; => &#39;success&#39;                        ]                ];                return view(&#39;test&#39;)->with($data);            }        }        </code>

3.创建对应的路由

<code>    Route::get('test', '[email protected]');</code>

4.打开对应的页面,如果你没有出错的 应该如下图所示
image

这样整个过程就完成了,当然有什么问题可以下面留言。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools