laravel学习之旅

前言:之前写了二篇YII2.0的基本mvc操作,所以,打算laravel也来这一下

*安装现在一般都用composer安装,这里就不讲述了*

 

一、熟悉laravel

(1)如果看到下面这个页面,就说明你已经安装好框架了

 

 

 

(2)认识一下目录结构

 

 

二、mvc操作

*每次增加一个控制器与方法,都要增加路由*

\routes\web.php

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)显示方法与接收方法

    /**
     * 列表
     * @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),自己在原来的基础上封装了一下

<?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

posted @ 2019-10-16 16:43  FcLwYf  阅读(234)  评论(0编辑  收藏  举报