前言:之前写了二篇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

laravel学习之旅的更多相关文章

  1. Laravel学习之旅(三)

    视图 一.怎么新建视图: 1.视图默认存放路径:resources/views: 2.laravel模板支持原生的PHP,直接可以在resources/views新建一个PHP文件,例如: index ...

  2. Laravel学习之旅(二)

    控制器 一.怎么编写控制器? 1.控制器文件存放路径:app\Http\Controllers: 2.命名规范如:TestController.php 3.完整的控制器例子如下: <?php n ...

  3. Laravel学习之旅(一)

    路由 1.简介:简单的说就是将用户的请求转发给相应的程序进行处理: 2.作用:就是建立url和程序之间的映射. 3.请求类型:get.post.put.patch.delete 相比于thinkphp ...

  4. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  5. Hadoop学习之旅二:HDFS

    本文基于Hadoop1.X 概述 分布式文件系统主要用来解决如下几个问题: 读写大文件 加速运算 对于某些体积巨大的文件,比如其大小超过了计算机文件系统所能存放的最大限制或者是其大小甚至超过了计算机整 ...

  6. WCF学习之旅—第三个示例之二(二十八)

    上接WCF学习之旅—第三个示例之一(二十七) 五.在项目BookMgr.Model创建实体类数据 第一步,安装Entity Framework 1)  使用NuGet下载最新版的Entity Fram ...

  7. WCF学习之旅—第三个示例之三(二十九)

    上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) 在上一篇文章中我们创建了实体对象与接口协定,在这一篇文章中我们来学习如何创建WCF的服务端代码.具体步骤见下面. ...

  8. WCF学习之旅—WCF服务部署到IIS7.5(九)

    上接   WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...

  9. WCF学习之旅—WCF服务部署到应用程序(十)

    上接  WCF学习之旅—WCF寄宿前的准备(八) WCF学习之旅—WCF服务部署到IIS7.5(九) 五.控制台应用程序宿主 (1) 在解决方案下新建控制台输出项目 ConsoleHosting.如下 ...

随机推荐

  1. python 中的while循环、格式化、编码初始

    while循环 循环:不断重复着某件事就是循环 while 关键字 死循环:while True: ​ 循环体 while True: # 死循环# print("坚强")# pr ...

  2. 第12讲-Java中的IO操作及对象的序列化与反序列化

    1.知识点 1.1.课程回顾 1.2.本章重点 1.2.1  io操作 1.2.2  对象的序列化与反序列化 2.具体内容 2.1.Java IO 2.1.1.什么是IO IO其实就是输入.输出 I ...

  3. Seaborn数据可视化入门

    在本节学习中,我们使用Seaborn作为数据可视化的入门工具 Seaborn的官方网址如下:http://seaborn.pydata.org 一:definition Seaborn is a Py ...

  4. Go语言基础之文件操作

    本文主要介绍了Go语言中文件读写的相关操作. 文件是什么? 计算机中的文件是存储在外部介质(通常是磁盘)上的数据集合,文件分为文本文件和二进制文件. 打开和关闭文件 os.Open()函数能够打开一个 ...

  5. java对象与json字符串的互相转换

    java对象与json字符串的互相转换 1.采用 net.sf.json.JSONObject maven依赖包: <dependency> <groupId>net.sf.j ...

  6. Could not calculate build plan :lugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of

    eclipse中新建maven项目,出现 Could not calculate build plan :lugin org.apache.maven.plugins:maven-resources- ...

  7. 一道算法问题:一幢 200 层的大楼,给你两个鸡蛋. 如果在第 n 层扔下鸡蛋,鸡蛋不碎,那么从前 n-1 层扔鸡蛋都不碎. 这两只鸡蛋一模一样,不碎的话可以扔无数次. 已知鸡蛋在0层扔不会碎. 提出一个策略, 要保证能测出鸡蛋恰好会碎的楼层, 并使此策略在最坏情况下所扔次数最少.

    今晚要参加网易的笔试,所以一直在刷题,刷到这个题的时候觉得自己的思路很模糊,就去网上百度了一下,找到一个大神给的解决方案: 如下: (http://ppwwyyxx.com/2013/Problem- ...

  8. WebGL简易教程(四):颜色

    目录 1. 概述 2. 示例:绘制三角形 1) 数据的组织 2) varying变量 3. 结果 4. 理解 1) 图形装配和光栅化 2) 内插过程 5. 参考 1. 概述 在上一篇教程<Web ...

  9. 连drawable目录都没搞明白就想开发APP?

      我是一个善良的搬运工,关于drawable,来看看这位的博客吧: https://blog.csdn.net/xuaho0907/article/details/72848520   hiahia ...

  10. charles 设置为chrome代理

    本文参考:charles 设置为chrome代理 将charles设置为chrome的代理 需要注意的是,Chrome 和 Firefox 浏览器并不一定使用的就是本机,可能是一些代理工具,而 Cha ...