博主信息
博文 37
粉丝 0
评论 1
访问量 37185
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
分别创建控制器、视图、路由文件和控制器中模拟数据,并把数据渲染到视图中 以及使用@include将页面的header部分放到public/header.php中-2019-11-04
H先生
原创
926人浏览过

1、分别创建控制器、视图、路由文件


实例

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Home extends Controller
{
    public function index(){
        $data['title'] = '2019大阅兵';
        return view('admin/home/index',$data);
    }

}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


实例

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/304525', function () {
    // 读取数据
    $article = array('title'=>'2019国庆大阅兵','detail'=>'奖励上级阿萨德老费劲啊胜利大街爱上了就发了房间里睡大觉','auth'=>array('username'=>'admin','age'=>18));
    return view('article/detail',$article);
});

Route::get('/home','home@index');

Route::get('/article/detail',function (){
    return ('<div style="margin: 10px auto;text-align:center; font-weight: bold;color: red;">新闻列表</div>');
});

Route::get('/admin/home/index','admin\Home@index');
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例





实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div style="text-align: center;">{{$title}}</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

1.png









2、在控制器中模拟数据,并把数据渲染到视图中



实例

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Home extends Controller
{
    public function index(){
        $data['navs'] = array(array('title'=>'企业logo','url'=>'/'),array('title'=>'新闻资讯','url'=>'/'),array('title'=>'最新产品','url'=>'/'));
        return view('admin/home/index',$data);
    }

}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


1.png








 3、使用@include将页面的header部分放到public/header.php中

1.png


实例

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>企业站</title>
    <style>
        .header{background-color: #000;width: 100%;height: 50px;
            text-align: center;}
        .header a{line-height: 50px;color: #fff;margin: 0 20px;text-decoration: none;}
        .nav-left{width: 200px;height: 500px;background-color: red;float:left;}
        .news{width: 300px;height: 500px;background-color: green;float:left;}
    </style>
</head>
<body>
    {{--header导航--}}
    @include('admin/public/header')
    {{--left导航--}}
    <div class="nav-left"></div>

    {{--right 文字列表--}}
    <div class="news"></div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例



实例

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class Home extends Controller
{
    public function index(){
        $data['navs'] = array(array('title'=>'企业logo','url'=>'/'),array('title'=>'新闻资讯','url'=>'/'),array('title'=>'最新产品','url'=>'/'));
        return view('admin/home/index',$data);
    }

}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


实例

<div class="header">
    @foreach($navs as $nav){
    <a href="{{$nav['url']}}">{{$nav['title']}}</a>
    }
    @endforeach
</div>

运行实例 »

点击 "运行实例" 按钮查看在线实例





批改状态:合格

老师批语:写得很认真, 命令创建效率和正确率要优于手工
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学