In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
小编给大家分享一下laravel辅助函数怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
用法:1、dd()函数用于打印出给定的变量和结束脚本的运行,语法为"dd(变量)";2、asset()函数用于引入静态文件,生成一个url,语法为"asset(文件路径)";3、"base_path()"函数用于获取项目根目录路径。
本教程操作环境:windows10系统、Laravel6版,DELL G3电脑。
laravel辅助函数的用法有哪些
laravel中一些辅助函数
1.dd(),打印函数
//辅助函数 public function help() { dd('test'); 打印test,相当于dump()+die(),不会执行后面的return return 123; }
2.数组操作Arr,要引入Illuminate\Support\Arr
//辅助函数 public function help() { $data = Arr::collapse([[1,2,3],[4,5,6]]); return $data; //输出 [1,2,3,4,5,6] 合并数组 } //辅助函数 public function help() { $data = ['a'=>1, 'b'=>2]; $data = Arr::except($data, ['a']); return $data; //输出 ["b":2] 删除某个键值 }
3.app_path(),获取app路径
//辅助函数 public function help() { $data = app_path(); return $data; }
4.base_path(),获取项目根目录路径
//辅助函数 public function help() { $data = base_path(); return $data; }
5.config_path(),获取config路径
//辅助函数 public function help() { $data = config_path(); return $data; }
6.database_path(),获取database路径
//辅助函数 public function help() { $data = database_path(); return $data; }
7.public_path(),获取public路径
//辅助函数 public function help() { $data = public_path(); return $data; }
8字符串操作Str,要引入Illuminate\Support\Str
//辅助函数 public function help() { $data = Str::after('today is sunday','is'); return $data; //输出 sunday 获取某个字符串之后的字符串 } //辅助函数 public function help() { $data = Str::before('today is sunday','is'); return $data; //输出 today 获取某个字符串之前的字符串 } //辅助函数 public function help() { $data = Str::between('today is sunday','today','sunday'); return $data; //输出 is 返回之间的字符串 } //辅助函数 public function help() { $data = Str::contains('today is sunday'); return $data; //输出 true 判断是否存在某个字符串 } //辅助函数 public function help() { $data = Str::endsWith('today is sunday', 'y'); return $data; //输出 true 判断以某个字符串结尾 } //辅助函数 public function help() { $data = Str::length('today is sunday'); return $data; //输出 15 字符串长度 } //辅助函数 public function help() { dd(Str::limit('today is sunday',8)); //打印 today is... } //辅助函数 public function help() { dd(Str::lower('TODAY is sunday',8)); //转换小写 } //辅助函数 public function help() { dd(Str::random()); //随机字符串 } //辅助函数 public function help() { dd(Str::of('today is sunday')->append(', happy')); //链式操作,追加 } //辅助函数 public function help() { dd(Str::of('today is sunday')->before('sunday')); //链式操作,返回字符串之前的字符串 }
9.action(),生成url
//辅助函数 public function help() { $url = action([HomeController::class, 'index']); return $url; }
10.asset(),生成url
//辅助函数 public function help() { $url = asset('img/abc.jpg'); return $url; }
11.env(),获取环境配置
//辅助函数 public function help() { $data = env('APP_ENV'); return $data; }
12.info(),打印日志
//辅助函数 public function help() { $data = info('this is a test log info'); //向storage/logs/laravel.log中插入一条日志 return $data; }
13.redirect(),跳转路由
//辅助函数 public function help() { redirect('/'); }以上是"laravel辅助函数怎么用"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.