Laravel结构
频繁的操作经常在这几个地方
模型:app\Model
控制器:app\Http
视图:resources\views
路由:routes
扩展方法都在vendor文件夹
Composer的使用
Composer 安装/更新
composer install
composer update
Composer主要用于下载添加扩展
例如:
composer require mews/captcha
composer require Jpush/Jpush
数据库
PHP语法千篇一律,数据库的接入会有些不同,需要更多的去看例子,实践。
下面链接是Laravel 5.7的开发文档,要理解它的对接数据库的语法。
Laravel 5.7
路由
路由就是Laravel用来指定某接口方法在哪个文件里的哪个方法。
例如:
Route::get('equipmentmodel', 'EquipmentModelController@index');
Route::post('equipment/white', 'EquipmentController@white');
控制器
控制器就是主要的逻辑代码,里面有一个个方法对应每一个功能,然后调用model去进行数据库操作。
<?php
namespace App\Http\Controllers;
use App\Lib\Redis;
class EquipmentController extends BaseController{
public function __construct() {
parent::__construct ();
$this->equipment = new \App\Lib\Equipment();
}
//列表
function index(){
//创建一个业务类对像(在业务对像的构造函数中已关联相关model类)
$equipment = $this->equipment;
//筛选标题,在这里赋值后,model中通过业务类对像可以获取到
$equipment->search['title'] = $this->request->input('title','');
$equipment->search['start'] = $this->request->input('start','')
$firmware = new \App\Lib\Firmware();
$this->_Result['firmware'] = $firmware->getModel()->getFirmware();
$model = new \App\Lib\EquipmentModel();
//从编辑页中返回的时候需要回填表单
$this->_Result['param'] = $equipment->search;
if( $this->request->input('isexport','')==1){
return $this->export($equipment->search);
}
//自动锁车配置的按钮
$is_auto=$this->equipment->getModel()->simpleGetRow();
$this->_Result['is_auto']=$is_auto['is_auto'];
//基本用法,此用法会按业务类的名称去关联列表类
$this->_Result['pagelistting'] = $equipment->createPageListting('pagelistting',site_url('admin/equipment?type='. $equipment->search['type']),true)->ajaxOutPut();
return view('equipment/equipmentpagelist',$this->_Result);
}
//修改
public function edit(){
}
/**
* 删除
*/
public function del() {
}
/*
* gcc
*/
public function substrSerialNumber($number)
{
}
public function detail(){
}
}
模型
model的方法是直接跟数据库相关的语句操作,增删改查在这里操作。
<?php
namespace App\Model;
use Qusu\Lib\Core\DBModel;
class AdvertModel extends DBModel\DBModel{
/**
* 与模型关联的数据表
*
* @var string
*/
protected $table = 'dlc_advert';
protected $primaryKey='id';
protected $created_at='createtime';
protected $updated_at='updatetime';
public function listingData(\Qusu\Lib\Core\Listing $listing){
$libobj = $listing->getContext();
$db = \DB::table($this->getFullTableName());
$db->select ( \DB::raw($this->getFullFieldName('*')) );
$query = $db->get();
return $this->getArray($query);
}
public function pageListingData(\App\Lib\Listing\AdvertPageListing $listing){
$libobj = $listing->getContext();
$db = \DB::table($this->getFullTableName());
$db->select ( \DB::raw('SQL_CALC_FOUND_ROWS '.$this->getFullFieldName('*')) )
->where('is_del','=',0)
->orderby('weight','desc');
if(isset($libobj->search) && getvalue($libobj->search, 'title')!=''){
$db->where($this->getFullFieldName('chinaTitle'),'like','%'.getvalue($libobj->search, 'title').'%');
$db->orwhere($this->getFullFieldName('franceTitle'),'like','%'.getvalue($libobj->search, 'title').'%');
$db->orwhere($this->getFullFieldName('koreaTitle'),'like','%'.getvalue($libobj->search, 'title').'%');
$offset = $listing->getOffset();
$limit = $listing->getCustomLimit();
if ($limit !== 0) {
$db->offset($offset)->limit ( $limit );
}
$query = $db->get();
return $this->getTotalList($query);
}
public function detail(\App\Lib\Advert $lib){
$db = \DB::table($this->getFullTableName());
$db->select ( \DB::raw('SQL_CALC_FOUND_ROWS '.$this->getFullFieldName('*')) );
$db->where($this->getFullFieldName('id'),'=',$lib->getId());
$data = $db->first();
if(!empty($data)){
return get_object_vars($data);
}
return array();
}
public static function adReleaseNum()
{
return self::where(['is_del'=>0,'is_show'=>1])->count();
}
}
Laravel框架的MVC模型是很好用的,要想深入了解还需要多实践多理解。