laravel學習之旅

前言:以前寫了二篇YII2.0的基本mvc操做,因此,打算laravel也來這一下php

*安裝如今通常都用composer安裝,這裏就不講述了*css

 

1、熟悉laravelhtml

(1)若是看到下面這個頁面,就說明你已經安裝好框架了laravel

 

 

 

(2)認識一下目錄結構web

 

 

2、mvc操做api

*每次增長一個控制器與方法,都要增長路由*數組

\routes\web.phpmvc

Route::get('/', function () {
    return view('welcome');
});

Route::get('/test', 'TestController@index');
Route::get('/test/add', 'TestController@add');
Route::post('/test/add', 'TestController@add');
Route::get('/test/saveshow/{a_id}', 'TestController@saveshow');
Route::post('/test/update', 'TestController@update');

 

(1)控制器(c)顯示方法與接收方法app

    /**
     * 列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $model = new Article();
        $condition = 'a_id>0';
        $list = $model->getArticleStrsList($condition);
        return view('test.index',['list'=>$list]);
    }

    /**
     * 增長
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request){
        $model = new Article();
        if($request->isMethod('post')){

            // 校驗
            $request->validate([
                'article_title' => 'required',
                'author' => 'required',
            ]);

            // 進行數據操做
            $article_title = $request->input('article_title','');
            $author = $request->input('author','');
            $insertData = array();
            $insertData['article_title'] = $article_title;
            $insertData['author'] = $author;
            $insertData['article_content'] = '';
            $model->addArticle($insertData);
            return redirect('test');

        }else{
            return view('test/add');
        }
    }

    /**
     * 更新顯示方法
     * @param Request $request
     */
    public function saveshow($a_id){
        // form參數請求
        $model = new Article();
        $info = $model->getOneById(array('a_id'=>$a_id));
        return view('test/saveshow',['info'=>$info]);
    }

    /**
     * 更新
     * @param Request $request
     */
    public function update(Request $request){
        // form參數請求
        $model = new Article();
        if($request->isMethod('post')){
            // 校驗
            $request->validate([
                'article_title' => 'required',
                'author' => 'required',
                'a_id'=>'required'
            ]);

            // 進行數據操做
            $article_title = $request->input('article_title','');
            $author = $request->input('author','');
            $a_id = $request->input('a_id');
            $updateData = array();
            $updateData['article_title'] = $article_title;
            $updateData['author'] = $author;
            $updateData['article_content'] = '';
            $model->editArticle($updateData,array('a_id'=>$a_id));
            return redirect('test');
        }
    }

(二)、模型(m),本身在原來的基礎上封裝了一下composer

<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Article extends Model
{

    public static $tableName = 'article';

    /**
     * 獲取商品列表
     * @param $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getArticleList($condition, $field = '*', $page = 0, $order = 'a_id desc'){
        $list = DB::table(self::$tableName)
            ->where($condition)
            ->orderBy($order)
            ->get();
        return $list;
    }

    /**
     * 獲取商品列表(經過原生字符串方式)
     * @param $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getArticleStrsList($condition, $field = '*', $offset = 0,$limit = 10, $order = 'a_id desc'){
        $list = DB::table(self::$tableName)
            ->whereRaw($condition)
            ->orderByRaw($order)
            ->offset($offset)
            ->limit($limit)
            ->get();
        return $list;
    }

    /**
     * 添加商品
     * @param type $data
     * @return type
     */
    public function addArticle($data)
    {
        return DB::table(self::$tableName)->insertGetId($data);
    }
    /**
     * 更新商品
     * @param type $data
     * @param type $condition
     * @return type
     */
    public function editArticle($data, $condition)
    {
        return DB::table(self::$tableName)->where($condition)->update($data);
    }

    /**
     * 刪除商品
     * @param array $condition 條件
     * @return type
     */
    public function delArticle($condition)
    {
        return DB::table(self::$tableName)->where($condition)->delete();
    }

    /**
     * 根據條件查詢一條商品
     *
     * @param array $condition 條件
     * @return array 一維數組
     */
    public function getOneById($condition)
    {
        return DB::table(self::$tableName)->where($condition)->first();
    }

    /**
     * 根據獲取總數
     * @param $condition
     * @return mixed
     */
    public function getCount($condition){
        return DB::table(self::$tableName)->where($condition)->count();
    }

    /**
     * 根據獲取總數(經過原生字符串方式)
     * @param $condition
     * @return mixed
     */
    public function getCountStrs($condition){
        return DB::table(self::$tableName)->whereRaw($condition)->count();
    }
}

 

(三)視圖(v),laravel下僞造跨站請求保護 CSRF,這個必定要加上

index.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
    <style>
        .list{
            width: 100%;
        }
        .list .l-l,.l-l-h{
            width:100%;
            text-align: center;
        }
        .l-l-h{
            font-weight: bold;
        }
        .list .l-v,.l-h{
            border: 1px solid #0b72b8;
            display:inline-block;
            width: 150px;
            float:left;
            padding: 0px;
            margin: 0px;
            text-align: center;
        }
        .l-h{
            font-weight: bold;
        }
        .clear{ clear:both}
    </style>
</head>
<body>
    <div>
        <a href="{{url('test/add')}}">添加</a>
    </div>
    <div class="list">
        <div class="l-l-h">
            <p class="l-h">
                標題
            </p>
            <p class="l-h">
                做者
            </p>
            <p class="l-h">
                操做
            </p>
            <div class="clear"></div>
        </div>
        @foreach ($list as $key=>$value)
            <div class="l-l">
                <p class="l-v">
                    {{$value->article_title}}
                </p>
                <p class="l-v">
                    {{$value->author}}
                </p>
                <p class="l-v">
                    <a href="{{url('test/saveshow',['a_id'=>$value->a_id])}}">編輯</a>
                </p>
                <div class="clear"></div>
            </div>
        @endforeach

    </div>
</body>
</html>

add.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel學習之旅</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
    <style>
        p{
            margin: 0px;
            padding: 0px;
        }
    </style>
</head>
<body>

<!-- /resources/views/post/add.blade.php -->

<h1>Create Post</h1>

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<!-- Create Post Form -->

    <div>
        <form action="{{url('test/add')}}" method="post">
            {{--這裏要寫上token,同樣要加上--}}
            {{ csrf_field() }}
            <p>
                <input type="text" name="article_title" placeholder="標題"/>
            </p>
            <p>
                <input type="text" name="author" placeholder="做者"/>
            </p>
            <p>
                <input type="submit" value="提交"/>
            </p>
        </form>
    </div>
</body>
</html>

saveshow.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel學習之旅</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
    <style>
        p{
            margin: 0px;
            padding: 0px;
        }
    </style>
</head>
<body>
    <div>
        <form action="{{url('test/update')}}" method="post">
            {{--這裏要寫上token,同樣要加上--}}
            {{ csrf_field() }}
            <input type="hidden" name="a_id" value="{{$info->a_id}}"/>
            <p>
                <input type="text" name="article_title" placeholder="標題" value="{{$info->article_title}}"/>
            </p>
            <p>
                <input type="text" name="author" placeholder="做者" value="{{$info->author}}"/>
            </p>
            <p>
                <input type="submit" value="提交"/>
            </p>
        </form>
    </div>
</body>
</html>

laravel注意事項:

  1. 訪問 laravel 出現 Parse error: syntax error, unexpected '?',說明php版本不適用當前laravel框架版本
  2. 視圖文件是以xxxx.blade.php爲後綴命名

操做網址:

laravel中文文檔:https://learnku.com/docs/laravel/6.x

相關文章
相關標籤/搜索