Laravel API笔记

    xiaoxiao2026-04-25  9

    1.namespace Carbon– DateTime 库

    获取日期的api /vendor/nesbot/carbon/src/Carbon/Carbon.php

    printf("Right now is %s", Carbon::now()->toDateTimeString()); printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); $tomorrow = Carbon::now()->addDay();//增加一天 $lastWeek = Carbon::now()->subWeek();//上个星期 $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);//2016年 $officialDate = Carbon::now()->toRFC2822String(); $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age; $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London'); $endOfWorld = Carbon::createFromDate(2012, 12, 21, 'GMT'); //总是以UTC对比 if (Carbon::now()->gte($endOfWorld)) { die(); } if (Carbon::now()->isWeekend()) { echo 'Party!'; } echo Carbon::now()->subMinutes(3)->diffForHumans(); // '3分钟之前'

    2.缓存

    $expiresAt = Carbon::now()->addMinutes(10); Cache::put('key', 'value', $expiresAt);

    永久存储

    Cache::forever('key', 'value');

    手动清掉

    Cache::forever('key', 'value');

    清掉所有缓存

    Cache::flush();

    3.安全认证机制

    Laravel 的目标就是要让实现认证机制变得简单。事实上,几乎所有的设置默认就已经完成了。有关认证的配置文件都放在 app/config/auth.php 里,而在这些文件里也都包含了良好的注释描述每一个选项的所对应的认证行为及动作。

    Laravel 默认在 app/models 文件夹内就包含了一个使用 Eloquent 认证驱动的User 模型。请记得在建立模型结构时,密码字段至少要有 60 个字串宽度。

    加密 >安全的 Bcrypt 哈希演算法: $password = Hash::make('secret'); 验证密码: if (Hash::check('secret', $hashedPassword)) { // The passwords match... }

    4.保护路由

    路由过滤器可让用户仅能访问特定的链接。Laravel 默认提供 auth 过滤器,其被定义在 app/filters.php 文件内。 保护特定路由

    Route::get(‘statue’, array(‘before’ => ‘auth’, function() { // Only authenticated users may enter… }));


    5.Response返回结果

    $response = Response::make($contents, $statusCode); $response->header('Content-Type', $value); return $response;

    带cookie的呢

    $cookie = Cookie::make('name', 'value'); return Response::make($content)->withCookie($cookie);
    转载请注明原文地址: https://ju.6miu.com/read-1309221.html
    最新回复(0)