• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

lararvel常用操作

武飞扬头像
胡萝卜的兔
帮助1

字符串操作

公共字符串

  $str = "我是一个处理字符串的函数";

after 方法将返回字符串中指定值后的所有内容。如果字符串中不存在这个值,它将返回整个字符串

$a = Str::of($str)->after("一个");
 return Result::success($a);

结果

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "处理字符串的函数"
}

afterLast 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值,它将返回整个字符串:

 $a = Str::of($str)->afterLast('串');

结果:

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "的函数"
}

append 方法为字符串附加上指定的值:

$a = Str::of($str)->append(",我在你后面跟着");
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "的函数"
}

ascii 方法尝试将字符串转换为 ASCII 值:

 $a = Str::of('ü')->ascii();

结果

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "u"
}

basename 方法将返回指定字符串的结尾部分

 $a = Str::of($str)->basename();

结果

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "u"
}

如果有必要,您亦可提供提供一个「扩展名」,将从尾部的组件中移除它。

$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');

// 'baz'

before 方法返回字符串中指定值之前的所有内容:

$slice = Str::of('This is my name')->before('my name');

// 'This is '

beforeLast 方法返回字符串中指定值最后一次出现前的所有内容

$a = Str::of(‘This is my is Is name’)->beforeLast(‘is’);

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "This is my "
}

camel 方法将指定字符串转换为 驼峰式 表示方法

$converted = Str::of('foo_bar')->camel();

// fooBar

contains 方法判断指定字符串中是否包含另一指定字符串(区分大小写):

$contains = Str::of('This is my name')->contains('my');

// true

您亦可以传递数组的值的形式来判断指定字符串是否包含数组中的任一值:

$contains = Str::of('This is my name')->contains(['my', 'foo']);

// true

containsAll 方法用于判断指定字符串是否包含指定数组中的所有值:

$containsAll = Str::of('This is my name')->containsAll(['my', 'name']);

// true

dirname 方法用于返回指定字符串的父级目录部分:

$string = Str::of('/foo/bar/baz')->dirname();

// '/foo/bar'

您亦可指定您想要从字符串中删除多少个目录级别,该参数是可选的:

$string = Str::of('/foo/bar/baz')->dirname(2);

// '/foo'

endsWith 方法用于判断指定字符串是否以另一指定字符串结尾:

$a = Str::of('This is my name')->endsWith('name');
// true
$a = Str::of('This is my name java')->endsWith('name');
// false

您亦可以传递数组的值的形式来判断指定字符串是否包含指定数组中的任一值:

 $a = Str::of('This is my name')->endsWith(['name', 'foo']);

// true

$a = Str::of('This is my name')->endsWith(['this', 'foo']);

// false

exactly 方法用于判断指定字符串是否与另一字符串完全匹配:

    $a = Str::of('Laravel')->exactly('Laravel');

// true
    $a = Str::of('Laravel1')->exactly('Laravel');

// false

finish 方法用于判断指定字符串末尾是否有特定字符,若没有,则将其添加到字符串末尾

$adjusted = Str::of('this/string')->finish('/');

// this/string/

$adjusted = Str::of('this/string/')->finish('/');

// this/string/

URL

action 方法为指定的控制器的 action 生成一个 URL:

$a = action([UserController::class, 'user']);

结果:

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/api/user"
}

您可以将路由参数作为第二个参数传递给这个方法:

$a = action([UserController::class, 'user'],['id'=>1]);

结果:

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/api/user?id=1"
}

asset 函数使用当前的请求协议( HTTP 或 HTTPS )来为资产生成 URL:

 $a = asset('img/photo.jpg');

结果:

{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/img/photo.jpg"
}

您可以配置 .env 文件中的 ASSET_URL 变量来设置资源的 URL 主机。
当您在诸如 Amazon S3 这样的外部服务上托管您的资产时,这将非常有用:

// ASSET_URL=http://example.com/assets

$url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg

route 函数为给定的路由名生成一个 URL:

users 必须存在
学新通

$a = route('users');
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/users"
}

您可以将路由参数作为该函数的第二个参数传递给它

$a = route('users', ['id' => 1]);
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/users?id=1"
}

默认情况下, route 函数将生成一个绝对的 URL 。如果您想要生成相对的 URL ,您可以将 false 作为该方法的第三个参数传递给它:

$a = route('users', ['id' => 1],false);
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "/users?id=1"
}

secure_asset 函数将使用 HTTPS 协议为资产生成一个 URL:

$url = secure_asset('img/photo.jpg');
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "https://127.0.0.1:8000/img/photo.jpg"
}

secure_url 函数将使用 HTTPS 为指定路径生成一个完整的 URL:

$a = secure_url('user/profile', ['age'=>18,'name'=>"jake"]);

$url = secure_url('user/profile');

$url = secure_url('user/profile', [1]);

url 函数将为指定路径生成一个完整的 URL :

$url = url('user/profile');

$url = url('user/profile', [1]);

如果没有指定路径,该方法将会返回一个 Illuminate\Routing\UrlGenerator 实例:

http://127.0.0.1:8000/str/after?id=10

获取当前url

$current = url()->current();
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/str/after"
}

获取全部url 包括参数

$full = url()->full();
{
    "success": true,
    "failed": false,
    "code": 0,
    "message": "成功",
    "data": "http://127.0.0.1:8000/str/after?id=10"
}

获取上一步的URL

$previous = url()->previous();

Laravel 与Vue 跨域

1.需要建立一个中间件

php artisan make:middleware Cors

学新通

  1. 在Cors.php 文件的handle里写入
   public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', '*');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'false');
        return $response;

    }

这里需要用星号(*),不然可能没有效果

  $response->header('Access-Control-Allow-Headers', '*');
  // 不能用下面的
  $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json');
  1. 需要在Kernel.php加入全局
\App\Http\Middleware\Cors::class

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgffbbb
系列文章
更多 icon
同类精品
更多 icon
继续加载