欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Laravel5.3初步使用三(View层、Controller层)

程序员文章站 2022-06-15 23:47:12
...

一、View层

1.前端制作好一些静态的文件,存放在public文件夹中,这些文件包括图片、js、css等
Laravel5.3初步使用三(View层、Controller层)

2.利用前端做好的页面,即html文件的代码,制作 *.blade.php模板页面或者普通php页面

3.当多个页面有相同的布局,比如分上中下,上部导航栏和底部网页信息栏在多个页面都差不多,只有中间内容页更改的情况下,我们可以做一个模板页,在View文件夹下新建一个**Common的文件夹
源码:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="{{asset('static/js/html5.js')}}"></script>
    <script type="text/javascript" src="{{asset('static/js/respond.min.js')}}"></script>
    <script type="text/javascript" src="{{asset('static/js/PIE_IE678.js')}}"></script>
    <![endif]-->
    <link href="{{asset('static/css/H-ui.css')}}" rel="stylesheet" type="text/css" />
    <link href="{{asset('static/css/H-ui.admin.css')}}" rel="stylesheet" type="text/css" />
    <link type="text/css" rel="stylesheet" href="{{asset('static/font/font-awesome.min.css')}}"/>
    <!--[if IE 7]>
    <link href="{{asset('static/font/font-awesome-ie7.min.css')}}" rel="stylesheet" type="text/css" />
    <![endif]-->
    <title>@yield('title')</title>
</head>
<body>
@section('nav')

@show
<div class="pd-20">

    @section('form')

    @show
</div>

@section('javascript')


<script type="text/javascript">
    $(".Huiform").Validform();
</script>


@show

</body>
</html>

4.引用第3步写好的母版
部分解释:
@extends() 继承布局页面
@section() …. @stop 重写内容代码

源码:

@extends('Common.layouts')

@section('nav')
    <nav class="Hui-breadcrumb">
        <i class="icon-home"></i> 首页
        <span class="c-gray en">&gt;</span> 管理员管理
        <span class="c-gray en">&gt;</span> 管理员列表
        <a class="btn btn-success radius r mr-20"
           style="line-height:1.6em;margin-top:3px"
           href="javascript:location.replace(location.href);" title="刷新" >
            <i class="icon-refresh"></i>
        </a>
    </nav>
@stop

@section('form')
    <div class="text-c">
        <form class="Huiform" method="get" action="{{url('adminmanager/createadmin')}}" >
            <input type="text" placeholder="帐号"
                   name="adminname"
                   autocomplete="off" value=""
                   maxlength="20"
                   class="input-text">
            <input type="password"
                   placeholder="密码"
                   autocomplete="off"
                   name="adminpwd1"
                   maxlength="32"
                   value="" class="input-text">
            <input type="password"
                   placeholder="确认密码"
                   autocomplete="off"
                   maxlength="32"
                   name="adminpwd2"
                   value="" class="input-text">
            <select class="select" name="admin-role" size="1">
                @foreach($sysroles as $sysrole)
                <option value="{{$sysrole->SRId}}">{{$sysrole->SRName}}</option>
                @endforeach
            </select>
            <button type="submit"  class="btn btn-success" id="" name="" >
                <i class="icon-plus"></i> 添加</button>
            @if (Session::has('adminaddtrue'))
                <p style="color: #bd362f;}}">
                    {{ Session::get('adminaddtrue') }}
                </p>
            @endif
            @if (Session::has('adminaddfalse'))
                <p style="color: #bd362f;}}">
                    {{ Session::get('adminaddfalse') }}
                </p>
            @endif

        </form>
    </div>
    <div class="cl pd-5 bg-1 bk-gray mt-20">
        <span class="l"><a href="javascript:;" onClick="datadel()" class="btn btn-danger radius"><i class="icon-trash"></i> 批量删除</a></span>
        <span class="r">共有数据:<strong>{{$musernum}}</strong></span>
    </div>
    <table class="table table-border table-bordered table-bg">
        <thead>
        <tr>
            <th scope="col" colspan="7">员工列表</th>
        </tr>
        <tr class="text-c">
            <th width="25"><input type="checkbox" name="" value=""></th>
            <th width="40">ID</th>
            <th width="150">登录名</th>
            <th>角色</th>
            <th width="130">加入时间</th>
            <th width="100">是否已启用</th>
            <th width="100">操作</th>
        </tr>
        </thead>
        <tbody>
        @for($i=0;$i<$musernum;$i++)
        <tr class="text-c">
            <td><input style="{{($muser[$i]->MuId==1?'display: none':'')}}" type="checkbox" value="{{$muser[$i]->MuId}}" name=""></td>
            <td>{{$muser[$i]->MuId}}</td>
            <td>{{$muser[$i]->MuName}}</td>
            <td>{{$arry[$i]}}</td>
            <td>{{$muser[$i]->Adddate}}</td>
            <td class="admin-status"><span class="label {{$muser[$i]->Mustop==0?'label-success':''}}">
                    {{$muser[$i]->Mustop==0?'已启用':'已停用'}}</span></td>
            <td class="f-14 admin-manage">
                <a style="text-decoration:none;{{$muser[$i]->MuId==1?'display: none':''}}"
                   onClick="if (confirm('确定要{{ ($muser[$i]->Mustop==0)?'停用':'启用' }}吗?') == false) return false;"
                   href="{{url('adminmanager/stopadmin',['id' => $muser[$i]->MuId])}}"
                   title="{{$muser[$i]->Mustop==0?'停用':'启用'}}">
                    <i class="icon-hand-{{$muser[$i]->Mustop==0?'down':'up'}}">
                    </i></a>
                <a title="编辑" href="javascript:;"
                   onClick="admin_role_edit('{{$muser[$i]->MuId}}','350','250','管理员编辑','{{url('adminmanager/updateadmin',['id' => $muser[$i]->MuId])}}')"
                   style="text-decoration:none;{{($muser[$i]->SRId==1)?'display: none':''}}">
                    <i class="icon-edit"></i>
                </a>
                <a style="text-decoration:none;{{$muser[$i]->MuId==1?'display: none':''}}" class="ml-5"
                   onClick="user_password_edit('{{$muser[$i]->MuId}}','370','250','修改密码','{{url('usercenter/modifypwd',['id' => $muser[$i]->MuId])}}')"
                   href="javascript:;" title="修改密码"><i class="icon-key"></i></a>
                <a title="删除" href="{{ url('adminmanager/deleteadmin', ['id' => $muser[$i]->MuId]) }}"
                   onclick="if (confirm('确定要删除吗?') == false) return false;"
                   class="ml-5" style="text-decoration:none;{{$muser[$i]->MuId==1?'display: none':''}}">
                    <i class="icon-trash"></i></a>

            </td>
        </tr>
        @endfor
        <!--<tr class="text-c">
            <td><input type="checkbox" value="2" name=""></td>
            <td>2</td>
            <td>zhangsan</td>
            <td>栏目编辑</td>
            <td>2014-6-11 11:11:42</td>
            <td class="admin-status"><span class="label">已停用</span></td>
            <td class="f-14 admin-manage">
                <a style="text-decoration:none"
                   onClick="admin_start(this,'10001')"
                   href="javascript:;" title="启用">
                    <i class="icon-hand-up"></i></a>
                <a title="编辑" href="javascript:;"
                   onClick="admin_edit('4','400','310','角色编辑','admin-edit.html')"
                   class="ml-5" style="text-decoration:none">
                    <i class="icon-edit"></i></a>
                <a title="删除" href="javascript:;"
                   onClick="admin_del(this,'1')" class="ml-5"
                   style="text-decoration:none">
                    <i class="icon-trash"></i></a>
            </td>
        </tr>-->
        </tbody>
    </table>

    <!-- 分页  -->
    <div>
        <div id="pageNav" class="pageNav">
            @if($musernum>0)
                {{ $muser->appends(Request::input())->render() }}
            @else
            @endif
        </div>
    </div>
@stop
@section('javascript')
<script type="text/javascript" src="{{asset('static/js/jquery.min.js')}}"></script>
<script type="text/javascript" src="{{asset('static/js/pagenav.cn.js')}}"></script>
<script type="text/javascript" src="{{asset('static/layer/layer.min.js')}}"></script>
<script type="text/javascript" src="{{asset('static/js/H-ui.js')}}"></script>
<script type="text/javascript" src="{{asset('static/js/H-ui.admin.js')}}"></script>
@stop

使用模板页的优势在于,html页面的头部和尾部的重复代码均不用在多个页面写,而且,布局相同的页面,在多个页面也不需要重复写代码


二、Controller层

1.普通使用,关联view层
源码:

<?php
namespace App\Http\Controllers;

use App\Muser;
use App\Role_power;
use Illuminate\Support\Facades\Session;

class IndexMenuController extends Controller{

    //主页 显示 用户名和权限菜单
    public function indexmexnu(){
        $name = Session::get('Muname');
        $srid = Session::get('SRId');
        $ary = Role_power::where('SRId',$srid)->pluck('RMId');
        $ary2=['none','none','none','none','none','none','none','none','none','none','none','none'];
        $ary3=count($ary);
        for($i=0;$i<count($ary);$i++)
        {
            for($j=1;$j<=12;$j++)
            {
                if($ary[$i]==$j)
                {
                    $ary2[$j-1]='';
                    break;
                }
            }
        }
        return view('menu.menu',[
            'name'=>$name,
            'ary2'=>$ary2,
        ]);
    }
    //主页显示的 欢迎页面
    public function welcome(){
        return view('indexCommon.welcome',[

        ]);
    }
}