不再赘述cakephp开发环境的搭建。
cakephp版本为3.x
bootstrap版本为bootstrap-3.3.5
昨天使用了bootstrap作为cakephp的界面设计。
如何使用bootstrap?
一、下载bootsrtap编译后的代码包http://v3.bootcss.com/getting-started/#download
二、解压下载的bootsrtap压缩文件,将里面的css文件和js文件拷贝到cakephp下的webroot存放css和js的默认文件下。
三、假设你使用默认的layout,也就是src文件夹下的layout中的default.ctp文件。在default.ctp <head>标签内添加bootstrap引用:
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<?= $this->Html->css('bootstrap.min.css') ?>
<?= $this->Html->script('bootstrap.min.js') ?>
关于$this->html,它是cakephp中的helper,帮助编写html文本,即<?= $this->Html->script('bootstrap.min.js') ?>
就相当于 <script src="js/bootstrap.min.js"></script>
这样就可以使用bootstrap的样式和js了。(可参考bootstrap源码中的example)
四、使用实例、
将default.ctp中的导航栏换成bootstrap样式
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"> <?= $this->html->image('favicon.ico') ?></a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">DropDown<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>
<?= $this->html->image('favicon.ico') ?>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $this->request->session()->read('User.email');?>
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
<li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<img src="/sensors/img/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
<span class="hidden-xs"><?php echo $this->request->session()->read('User.email');?></span>
</a>
<ul class="dropdown-menu">
<!-- User image -->
<li class="user-header" >
<img src="/sensors/img/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
<h4>
-- Web Developer --
<br />
<small>Member since Nov. 2012</small>
</h4>
</li>
<!-- Menu Body -->
<li class="user-body">
<div class="col-xs-4 text-center">
<a href="#">Followers</a>
</div>
<div class="col-xs-4 text-center">
<a href="#">Sales</a>
</div>
<div class="col-xs-4 text-center">
<a href="#">Friends</a>
</div>
</li>
<!-- Menu Footer-->
<li class="user-footer">
<div class="pull-left">
<a href="#" class="btn btn-default btn-flat">Profile</a>
</div>
<div class="pull-right">
<!-- <a href="#" class="btn btn-default btn-flat">Sign out</a>-->
<?= $this->html->link('退出',
['controller' => 'Users', 'action' => 'logout'],
['class' => 'btn btn-default btn-flat'])?>
</div>
</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
关于cakephp中的session使用:
$this->request->session()可以用于controller、model、views、component
例如用户登录需要存储session,可在login action中加入
$html->request->session()->write('user.email',$user['email']);
当需要在导航栏显示登录用户名时:
在default.ctp中需要显示的地方添加:
<?= $this->request->session()->read('user.email'); ?>
如果在view中需要利用cakephp的helper,并使用booststrap样式,那么可以写语句:
<?= $this->Flash->render('auth') ?>
<?= $this->html->css('signin.css')?>
<div class="container">
<?= $this->Form->create(null,array(
<span style="white-space:pre"> </span>'class'=>'form-signin'
) ) ?>
<h2 class="form-signin-heading">Please sign in</h2>
<?= $this->Form->input('email',array(
<span style="white-space:pre"> </span>'class'=>'form-control',
<span style="white-space:pre"> </span>
)) ?>
<?= $this->Form->input('password',array(
<span style="white-space:pre"> </span>'class'=>'form-control',
<span style="white-space:pre"> </span>'type'=> 'password'
)) ?>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<?= $this->Form->end() ?>
</div>