成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Laravel 5 系列入門教程(二)【最適合中國(guó)人的 Laravel 教程】

未東興 / 1075人閱讀

摘要:原文發(fā)表在我的個(gè)人網(wǎng)站系列入門教程二最適合中國(guó)人的教程本教程示例代碼見(jiàn)大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。


原文發(fā)表在我的個(gè)人網(wǎng)站:Laravel 5 系列入門教程(二)【最適合中國(guó)人的 Laravel 教程】


  本教程示例代碼見(jiàn):https://github.com/johnlui/Learn-Laravel-5

  
  

大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。

我們將改變學(xué)習(xí)路線,不再像 Laravel 4 教程那樣先構(gòu)建登錄系統(tǒng)。在本篇教程中,我們將一起構(gòu)建 Pages 的管理功能,嘗試 Laravel 的路由和 PHP 的命名空間。

1. 路由

Laravel 中的路由,跟其他 PHP 框架一樣,作用是把各種請(qǐng)求分流到各個(gè)控制器。

learnlaravel5/app/Http/routes.php 的末尾添加以下代碼:

phpRoute::group(["prefix" => "admin", "namespace" => "Admin"], function()
{
  Route::get("/", "AdminHomeController@index");
});

這表示創(chuàng)建了一個(gè)路由組。

"prefix" => "admin" 表示這個(gè)路由組的 url 前綴是 /admin,也就是說(shuō)中間那一行代碼 Route::get("/" 對(duì)應(yīng)的鏈接不是 http://fuck.io:88/ 而是 http://fuck.io:88/admin ,如果這段代碼是 Route::get("fuck" 的話,那么 URL 就應(yīng)該是 http://fuck.io:88/admin/fuck 。

"namespace" => "Admin" 表示下面的 AdminHomeController@index 不是在 AppHttpControllersAdminHomeController@index 而是在 AppHttpControllersAdminAdminHomeController@index,加上了一個(gè)命名空間的前綴。

如果你用過(guò) Laravel 4,會(huì)發(fā)現(xiàn) Laravel 5 的命名空間規(guī)劃比較怪異,這其實(shí)是一個(gè)非常大的進(jìn)步。Laravel 4 其實(shí)已經(jīng)全面引入了命名空間這個(gè)強(qiáng)大的特性,但是為了“降低學(xué)習(xí)成本”,把 路由、控制器、模型 的默認(rèn)命名空間全部設(shè)置成了頂級(jí)命名空間,這個(gè)舉動(dòng)反而讓很多人比較輕易地“上手”了 Laravel,但是在用了一段時(shí)間以后,還需要翻越一堵高墻,那就是命名空間,而且有了前面的“容易上手”的印象作為鋪墊,后期的學(xué)習(xí)會(huì)更加困難。Laravel 5 把命名空間全部隔開(kāi),控制器在 AppHttpControllers,模型在 App,讓我們?cè)趧偵鲜值臅r(shí)候就體驗(yàn)命名空間分離的感覺(jué),總體上其實(shí)是會(huì)降低學(xué)習(xí)成本的。

2. 控制器

我們可以使用 Artisan 非常方便地構(gòu)建控制器:

bashphp artisan make:controller Admin/AdminHomeController

得到 learnlaravel5/app/Http/Controllers/Admin/AdminHomeController.php 文件。

class AdminHomeController extends Controller { 上面增加一行:

phpuse AppPage;

修改 index() 的代碼如下:

phppublic function index()
{
  return view("AdminHome")->withPages(Page::all());
}

控制器中文文檔:http://laravel-china.org/docs/5.0/controllers

控制器中涉及到了許多的命名空間知識(shí),可以參考 PHP 命名空間 解惑。

3. 視圖

新建 learnlaravel5/resources/views/AdminHome.blade.php

php@extends("app")

@section("content")


后臺(tái)首頁(yè)
新增 @foreach ($pages as $page)

{{ $page->title }}

{{ $page->body }}

id."/edit") }}" class="btn btn-success">編輯
id) }}" method="POST" style="display: inline;">
@endforeach
@endsection

視圖的基本用法在此不再贅述,請(qǐng)閱讀中文文檔:http://laravel-china.org/docs/5.0/views

訪問(wèn) http://fuck.io:88/admin 得到如下頁(yè)面:

至此,包含 路由 》 控制器 》 模型 》 視圖 的整個(gè)流程都已經(jīng)完成。 4. 完成 Pages 管理功能

接下來(lái),我將記錄下我實(shí)現(xiàn) Pages 管理功能的過(guò)程,不再做過(guò)多的闡述。大家有問(wèn)題可以直接在本文下面留言,我會(huì)及時(shí)回復(fù)。

4.1 修改路由 learnlaravel5/app/Http/routes.php
phpRoute::group(["prefix" => "admin", "namespace" => "Admin"], function()
{
  Route::get("/", "AdminHomeController@index");
  Route::resource("pages", "PagesController");
});

此處增加了一條“資源控制器”,中文文檔地址:http://laravel-china.org/docs/5.0/controllers#restful-resource-controllers

4.2 創(chuàng)建 learnlaravel5/app/Http/Controllers/Admin/PagesController.php

運(yùn)行:

bashphp artisan make:controller Admin/PagesController
4.3 修改 learnlaravel5/app/Http/Controllers/Admin/PagesController.php 為:
phpvalidate($request, [
            "title" => "required|unique:pages|max:255",
            "body" => "required",
        ]);

        $page = new Page;
        $page->title = Input::get("title");
        $page->body = Input::get("body");
        $page->user_id = 1;//Auth::user()->id;

        if ($page->save()) {
            return Redirect::to("admin");
        } else {
            return Redirect::back()->withInput()->withErrors("保存失敗!");
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        return view("admin.pages.edit")->withPage(Page::find($id));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request,$id)
    {
        $this->validate($request, [
            "title" => "required|unique:pages,title,".$id."|max:255",
            "body" => "required",
        ]);

        $page = Page::find($id);
        $page->title = Input::get("title");
        $page->body = Input::get("body");
        $page->user_id = 1;//Auth::user()->id;

        if ($page->save()) {
            return Redirect::to("admin");
        } else {
            return Redirect::back()->withInput()->withErrors("保存失??!");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $page = Page::find($id);
        $page->delete();

        return Redirect::to("admin");
    }

}

4.4 創(chuàng)建視圖文件

首先在 learnlaravel5/resources/views 下創(chuàng)建 admin/pages 兩級(jí)文件夾。

然后創(chuàng)建 learnlaravel5/resources/views/admin/pages/create.blade.php:

php@extends("app")

@section("content")


新增 Page
@if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif


@endsection

之后創(chuàng)建 learnlaravel5/resources/views/admin/pages/edit.blade.php:

php@extends("app")

@section("content")


編輯 Page
@if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif
id) }}" method="POST">

@endsection
4.5 查看結(jié)果

后臺(tái)首頁(yè) http://fuck.io:88/admin :

新增 Page http://fuck.io:88/admin/pages/create :

編輯 Page http://fuck.io:88/admin/pages/1/edit :

頁(yè)面上的新增、編輯、刪除的功能均已經(jīng)完成,并且加入了表單驗(yàn)證,Pages 管理功能完成!


教程(二)代碼快照:https://github.com/johnlui/Learn-Laravel-5/archive/tutorial_2.zip


  下一步:Laravel 5 系列入門教程(三)【最適合中國(guó)人的 Laravel 教程】

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/30210.html

相關(guān)文章

  • 【完結(jié)】Laravel 4 系列入門教程適合國(guó)人Laravel教程

    摘要:完結(jié)教程一教程二教程三原文是富文本編輯器寫的,向不好轉(zhuǎn)換,大家點(diǎn)過(guò)去看吧。。。 【完結(jié)】 教程(一):http://lvwenhan.com/laravel/398.html 教程(二): http://lvwenhan.com/laravel/399.html 教程(三): http://lvwenhan.com/laravel/400.html 原文是富文本編輯器寫的...

    JinB 評(píng)論0 收藏0
  • Laravel 5 系列入門教程(四)【適合國(guó)人 Laravel 教程】【完結(jié)】

    摘要:原文發(fā)表在我的個(gè)人網(wǎng)站系列入門教程四最適合中國(guó)人的教程本教程示例代碼見(jiàn)大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。 原文發(fā)表在我的個(gè)人網(wǎng)站:Laravel 5 系列入門教程(四)【最適合中國(guó)人的 Laravel 教程】 本教程示例代碼見(jiàn):https://github.com/johnlui/Learn-Laravel-5 大家在任何地方卡住,最快捷...

    jsummer 評(píng)論0 收藏0
  • Laravel 5 系列入門教程(三)【適合國(guó)人 Laravel 教程

    摘要:原文發(fā)表在我的個(gè)人網(wǎng)站系列入門教程三最適合中國(guó)人的教程本教程示例代碼見(jiàn)大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。教程三代碼快照下一步系列入門教程四最適合中國(guó)人的教程完結(jié) 原文發(fā)表在我的個(gè)人網(wǎng)站:Laravel 5 系列入門教程(三)【最適合中國(guó)人的 Laravel 教程】 本教程示例代碼見(jiàn):https://github.com/johnlui/Learn-L...

    xcold 評(píng)論0 收藏0
  • Laravel 5 系列入門教程(一)【適合國(guó)人 Laravel 教程

    摘要:原文發(fā)表在我的個(gè)人網(wǎng)站系列入門教程一最適合中國(guó)人的教程本教程示例代碼見(jiàn)大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。在此我推薦一個(gè)全量中國(guó)鏡像。 原文發(fā)表在我的個(gè)人網(wǎng)站:Laravel 5 系列入門教程(一)【最適合中國(guó)人的 Laravel 教程】 本教程示例代碼見(jiàn):https://github.com/johnlui/Learn-Laravel-5 大...

    EscapedDog 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<