公共字符串
$str = "我是一个处理字符串的函数";
$a = Str::of($str)->after("一个");
return Result::success($a);
结果
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "处理字符串的函数"
}
$a = Str::of($str)->afterLast('串');
结果:
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "的函数"
}
$a = Str::of($str)->append(",我在你后面跟着");
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "的函数"
}
$a = Str::of('ü')->ascii();
结果
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "u"
}
$a = Str::of($str)->basename();
结果
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "u"
}
如果有必要,您亦可提供提供一个「扩展名」,将从尾部的组件中移除它。
$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
// 'baz'
$slice = Str::of('This is my name')->before('my name');
// 'This is '
$a = Str::of(‘This is my is Is name’)->beforeLast(‘is’);
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "This is my "
}
$converted = Str::of('foo_bar')->camel();
// fooBar
$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
$string = Str::of('/foo/bar/baz')->dirname();
// '/foo/bar'
您亦可指定您想要从字符串中删除多少个目录级别,该参数是可选的:
$string = Str::of('/foo/bar/baz')->dirname(2);
// '/foo'
$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
$a = Str::of('Laravel')->exactly('Laravel');
// true
$a = Str::of('Laravel1')->exactly('Laravel');
// false
$adjusted = Str::of('this/string')->finish('/');
// this/string/
$adjusted = Str::of('this/string/')->finish('/');
// this/string/
$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"
}
$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
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"
}
$url = secure_asset('img/photo.jpg');
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "https://127.0.0.1:8000/img/photo.jpg"
}
$a = secure_url('user/profile', ['age'=>18,'name'=>"jake"]);
$url = secure_url('user/profile');
$url = secure_url('user/profile', [1]);
$url = url('user/profile');
$url = url('user/profile', [1]);
如果没有指定路径,该方法将会返回一个 Illuminate\Routing\UrlGenerator 实例:
http://127.0.0.1:8000/str/after?id=10
$current = url()->current();
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "http://127.0.0.1:8000/str/after"
}
$full = url()->full();
{
"success": true,
"failed": false,
"code": 0,
"message": "成功",
"data": "http://127.0.0.1:8000/str/after?id=10"
}
$previous = url()->previous();
1.需要建立一个中间件
php artisan make:middleware Cors

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');
\App\Http\Middleware\Cors::class
