一、Laravel中文文档:https://learnku.com/docs/laravel/8.x/installation/9354 1.访问中文文档后,里面有详细的Laravel框架安装过程; 2.安装完成后,根据文档目录结构介绍配置文件进行配置; 3.配置完成相关配置文件后,就可以开始在Laravel框架上搭建自己的项目,例如配置config/database.php的数据库数据; 二、Laravel开发事项: 1.路由请求: (1)web.php:web路由
use App\Http\Controllers\Admin\Login;
Route::get('Admin/Admin', [Login::class, 'LoginList']);
Route::post('Admin/Admin', [Login::class, 'LoginList']);
Route::put('Admin/Admin', [Login::class, 'LoginList']);
Route::any('Admin/Admin', [Login::class, 'LoginList']);
Route::prefix('Index')->group(function(){
Route::any('/Validity', [WebEntrance::class, 'Validity']);
});
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
return $postId . '-' . $commentId;
});
Route::view('/welcome', 'welcome');
(2)api.php:api路由 2.Request请求
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$data=$request->all();
$name = $request->input('name');
return view('Index/ProductCenter.Editor',['data'=>$data]);
}
}
3.Cookie与Session用法差异: (1)Laravel保存Cookie:
引入命名空间:use Illuminate\Support\Facades\Cookie;
保存cookie:
cookie::queue('u_id', $arr['UserData'][0]['u_id'],10080);10080是分,不是秒,区别于cookie与$_COOKIE用法,而且Laravel不支持cookie()与$_COOKIE[]原生用法存储cookie
获取cookie
Cookie::get('u_id');Laravel不支持cookie()与$_COOKIE[]原生用法存储cookie
(3)session保存和获取
session基本使用
获取session中的所有数据
$request->session()->all();
$value = session('key');
存储数据到session...不支持原生session()方法进行存储
session(['key' => 'value']);
$request->session()->put('key', 'value');
push 方法可用于推送数据到值为数组的session
$request->session()->push('user.teams', 'developers');
删除session中的数据项
$request->session()->forget('key');
$request->session()->flush();
三、中间键的使用
在app/Http/Middleware 目录下创建一个新的中间件类 CheckAge
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
...
\App\Http\Middleware\CheckAge::class,
];
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'kuayu' => \App\Http\Middleware\CrossDomaine::class,
];
Route::group(['middleware'=>['kuayu']],function(){
Route::any('Admin/Admin', [Login::class, 'LoginList']);
});
四、Laravel的DB查询用法
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name;
}
$users = DB::table('users')->select('字段')->get()->map(function ($value){return (array)$value; })
->toArray();
$user = DB::table('users')->where('name', '学院君')->first();
echo $user->name;
$email = DB::table('users')->where('name', '学院君')->value('email');
$user = DB::table('users')->find(3);
public function RegisterData($code){
return DB::table('table1 AS a')
->leftJoin('table2 AS b','a.id','=','b.b_id')
->select('a.id','a.o_id','b.u_id','b.p_id',DB::Raw('concat(c.g_name,"(",b.g_describe,")") AS pay_desc' ))
->where('a.registration_code',$code)
->get()
->map(function ($value){return (array)$value; })
->toArray();
}
public function GetLeaveMessage($page_num,$rows){
return DB::table('table')
->select()
->orderBy('id','desc')
->paginate($rows,['id'],'page',$page_num);
}
$Message=$Model->GetLeaveMessage($page_num,$rows);
$total=$Message->total();
$data=$Message->items();
DB::table('table')->insertGetId($data);
DB::table('table')->insert($data);
DB::table('table')->where($where)->update($data)
DB::table('table')->where($where)->delete($data)
|