採用thinkphp架構實現添加管理員功能

來源:互聯網
上載者:User

標籤:

      最近由於忙於期中和期末考試沒有寫新的隨筆了,另外內心也在糾結要不要考研,直到昨天終於痛下決心,才突然間覺得豁然開朗。

  由於做老師留的課程設計作業採用thinkPHP架構頻繁,最近的幾篇隨筆將都從thinkPHP架構的使用上著筆,好了,廢話不多說,下面是乾貨。

  這篇文章將圍繞採用thinkPHP架構   向資料庫中添加資料   和   在網頁中顯示   這兩項功能進行展示。

  目的:在add頁添加資料後在lists頁進行顯示(注意:由於thinkPHP架構已經將list欄位佔用,因此在檔案命名時不得使用形如“list.html”的命名方式)

預期頁面:    

下面就利用MVC架構設計模式對其進行實現

首先利用表單提交方式實現V視圖部分,代碼如下:

<form role="form" method="post" action="__MODULE__/Admin/User/doAdd">                  <div class="input-group"> <span class="input-group-addon">用<img src="__PUBLIC__/end/images/em.png"  width="6" height="20">戶<img src="__PUBLIC__/end/images/em.png"  width="6" height="20">名:</span>                    <input type="text" class="form-control" placeholder="" name="username">                  </div>                  <div class="input-group "> <span class="input-group-addon" for="inputWarning1">真實姓名:</span>                    <input type="text" class="form-control" placeholder="" id="input" name="realname">                  </div>                  <div class="input-group"> <span class="input-group-addon">手機號碼:</span>                    <input type="text" class="form-control" placeholder="" name="telphone">                  </div>                  <div class="input-group"> <span class="input-group-addon">電子郵箱:</span>                    <input type="text" class="form-control" placeholder="" name="email">                  </div>                  <div class="input-group"> <span class="input-group-addon">添加時間:</span>                    <input type="text" class="form-control" placeholder="2014-05-22" name="resgistertime">                  </div>                <div class="input-group"> <span class="input-group-addon">設定密碼:</span>                    <input type="text" class="form-control" placeholder="123456"  name="password">                  </div>                <div class="input-group"> <span class="input-group-addon">確認密碼:</span>                    <input type="text" class="form-control" placeholder="123456" name="repassword">                  </div>                  <div class="input-group">                    <button type="submit" class="btn btn-primary "> &nbsp;&nbsp;保<img src="__PUBLIC__/end/images/em.png"  width="20" height="20">存&nbsp;&nbsp;</button>                  </div>                </form>

接下來是M模式部分,個人目前對這一部分的理解是    用來嚴重添加資料的合法性和給出錯誤提示   。實現代碼如下:

<?phpnamespace Admin\Model;use Think\Model;class AdminUsersModel extends Model {    public $_validate = array (        array("username", "require", "使用者名稱不可為空"),        array("realname", "require", "真實姓名不可為空"),        array("password", "require", "密碼不可為空"),        array("repassword", "require", "確認密碼不可為空"),        array("telphone", "require", "電話不可為空"),        array("email", "require", "郵箱不可為空"),        array("resgistertime", "require", "註冊時間不可為空")    );}

最後是純粹的邏輯C控制器部分啦,實現代碼如下:

public function add(){    $this->display();}public function doAdd(){    if (!IS_POST) {        exit("bad request!");    }    $adminUsersModel  = D("AdminUsers");    if (!$adminUsersModel->create()) {        $this->error($adminUsersModel->getError());    }    if ($adminUsersModel->add()) {                                  $this->success("添加成功!",U("Admin/User/lists"));    }    else{        $this->error("添加失敗!");    }    }

      以上就是整個實現過程了。

  友情連結thinkPHP參考手冊:    http://document.thinkphp.cn/manual_3_2.html

 

採用thinkphp架構實現添加管理員功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.