(五)——Laravel RESTful模板化

2018-02-24 15:43 更新

原文出處:https://www.phodal.com/blog/bare-minimum-iot-system-restful-template/

下面這部分來自于之前的博客,這里就不多加論述了。

這個(gè)也就是我們要的模板,

修改Create()

public function create()
    {
        $maxid=Athomes::max('id');
        return View::make('athome.create')->with('maxid',$maxid);
    }

這里需要在app/views/創(chuàng)建一個(gè)athome里面創(chuàng)建一個(gè)create.blade.php,至于maxid,暫時(shí)還不需要,后面會(huì)用到show。如果只需要模板,可以簡(jiǎn)化為

 public function create()
    {
        return View::make('athome.create');
    }

這里只是對(duì)其中代碼的進(jìn)行一下說明。

創(chuàng)建表單

創(chuàng)建表單之前

由于使用到了bootstrap以及bootstrap-select,記得添加css。

 <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
 <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />

以及javascript

<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/jquery.min.js')?>"></script>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/bootstrap.min.js') ?>"></script>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/script>
<script>
  $('.selectpicker').selectpicker();
</script>

創(chuàng)建表單

這里用到的是之前提到的那個(gè)作者寫下的,稍微修改了一下。

  <div class="row-fluid">
    {{ HTML::ul($errors->all()) }}
    {{ Form::open(array('url' => 'athome')) }}
       <div class="form-group">
            {{ Form::label('led1', '開關(guān)1') }}
            {{ Form::select('led1',array('關(guān)','開'),$selected=NULL,array('class'=>'selectpicker')) }}
        </div>
        <div class="form-group">
            {{ Form::label('sensors1', 'sensors1') }}
            {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
        </div>
        <div class="form-group">
            {{ Form::label('sensors2', 'sensors2') }}
            {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
        </div>
        <div class="form-group">
            {{ Form::label('temperature', 'temperature') }}
            {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
        </div>
        {{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
    {{ Form::close() }}
    </div>

開關(guān)一開始打算用checkbox,加上bootstrap-switch實(shí)現(xiàn)

<div id="dimension-switch" class="make-switch has-switch">
    <div class="switch-animate switch-on">
        <span class="switch-left">ON</span><label>?</label><span class="switch-right">OFF</span>
    </div>
</div>

弱弱地覺得還是沒掌握好的節(jié)奏,所以最后用select來實(shí)現(xiàn)。

還需要修改一下之前的create(),添加一行

return Redirect::to('athome');

也就是添加完后,重定向到首頁(yè)查看,最后例子給出的create如下

  public function store()
    {
        $rules = array(
            'led1'=>'required',
            'sensors1' => 'required|numeric|Min:-50|Max:80',
            'sensors2' => 'required|numeric|Min:-50|Max:80',
            'temperature' => 'required|numeric|Min:-50|Max:80'
        );
        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
            return Redirect::to('athome/create')
                ->withErrors($validator);
        } else {
            // store
            $nerd = new Athomes;
            $nerd->sensors1       = Input::get('sensors1');
            $nerd->sensors2       = Input::get('sensors2');
            $nerd->temperature    = Input::get('temperature');
            $nerd->led1            = Input::get('led1');
            $nerd->save();
        Session::flash('message', 'Successfully created athome!');
        return Redirect::to('athome');
    }
}

編輯edit

完整的blade模板文件

<!DOCTYPE html lang="zh-cn">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="">
        <meta name="viewport" content="width=device-width">
        <meta name="description" content="">
        <title>@yield('title')</title>
        <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap.min.css') ?>" />
        <link rel="stylesheet" type="text/css" href="<?= url('css/bootstrap-select.min.css') ?>" />
        <link rel="stylesheet" href="<?= url('css/justified-nav.css') ?>" type="text/css" media="screen" />
    </head>
<body>
<div class="container">
<div class="container">
  <div class="row-fluid">
<h1>Edit {{ $athome->id }}</h1>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::model($athome, array('route' => array('athome.update', $athome->id), 'method' => 'PUT')) }}
        <div class="form-group">
            {{ Form::label('led1', '開關(guān)1') }}
            {{ Form::select('led1',array('關(guān)','開'),$selected=NULL,array('class'=>'selectpicker')) }}
        </div>
        <div class="form-group">
            {{ Form::label('sensors1', '傳感器1') }}
            {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
        </div>
        <div class="form-group">
            {{ Form::label('sensors2', '傳感器2') }}
            {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
        </div>
        <div class="form-group">
            {{ Form::label('temperature', '溫度傳感器') }}
            {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
        </div>
    {{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
    </div>
</div>
<div class="footer">
        <p>? Company 2013</p>
      </div>
</div>
</div>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/jquery.min.js')?>"></script>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/bootstrap.min.js') ?>"></script>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/script>
<script>
 $('.selectpicker').selectpicker();
 </script>
<script type="text/javascript" src="https://atts.w3cschool.cn/attachments/image/cimg/log.js') ?>"></script>
</body>
</html>
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)