前言:之前写了二篇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
Laravel class="list">class="l-l-h">@foreach ($list as $key=>$value)class="l-h"> 标题
class="l-h"> 作者
class="l-h"> 操作
class="clear">class="l-l">@endforeachclass="l-v"> {{$value->article_title}}
class="l-v"> {{$value->author}}
class="l-v"> $value->a_id])}}">编辑
class="clear">
add.blade.php
Laravel学习之旅 Create Post
@if ($errors->any())class="alert alert-danger">@endif@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach
saveshow.blade.php
Laravel学习之旅
laravel注意事项:
- 访问 laravel 出现 Parse error: syntax error, unexpected '?',说明php版本不适用当前laravel框架版本
- 视图文件是以xxxx.blade.php为后缀命名
操作网址:
laravel中文文档:https://learnku.com/docs/laravel/6.x