Home > Article > PHP Framework > What are the uses of laravel helper functions?
Usage: 1. The dd() function is used to print out the given variables and end the running of the script. The syntax is "dd(variable)"; 2. The asset() function is used to introduce static files and generate A url, the syntax is "asset (file path)"; 3. The "base_path()" function is used to obtain the project root directory path.
The operating environment of this tutorial: Windows 10 system, Laravel 6 version, DELL G3 computer.
Some auxiliary functions in laravel
1.dd(), printing function
//辅助函数 public function help() { dd('test'); 打印test,相当于dump()+die(),不会执行后面的return return 123; }
2.Array operation Arr , to introduce 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(), get the app path
//辅助函数 public function help() { $data = app_path(); return $data; }
4.base_path(), get the project root directory path
//辅助函数 public function help() { $data = base_path(); return $data; }
5 .config_path(), get the config path
//辅助函数 public function help() { $data = config_path(); return $data; }
6.database_path(), get the database path
//辅助函数 public function help() { $data = database_path(); return $data; }
7.public_path(), get the public path
//辅助函数 public function help() { $data = public_path(); return $data; }
8 string To operate Str, introduce 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(), generate url
//辅助函数 public function help() { $url = action([HomeController::class, 'index']); return $url; }
10.asset(), and generate url
//辅助函数 public function help() { $url = asset('img/abc.jpg'); return $url; }
11.env (), get environment configuration
//辅助函数 public function help() { $data = env('APP_ENV'); return $data; }
12.info(), print log
//辅助函数 public function help() { $data = info('this is a test log info'); //向storage/logs/laravel.log中插入一条日志 return $data; }
13.redirect(), jump route
//辅助函数 public function help() { redirect('/'); }
[Related recommendations: laravel video tutorial】
The above is the detailed content of What are the uses of laravel helper functions?. For more information, please follow other related articles on the PHP Chinese website!