Home > PHP Framework > Laravel > What are the uses of laravel helper functions?

What are the uses of laravel helper functions?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2022-02-15 10:49:30
Original
2740 people have browsed it

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.

What are the uses of laravel helper functions?

The operating environment of this tutorial: Windows 10 system, Laravel 6 version, DELL G3 computer.

What are the usages of laravel auxiliary functions

Some auxiliary functions in laravel

1.dd(), printing function

//辅助函数
    public function help()
    {
        dd('test');    打印test,相当于dump()+die(),不会执行后面的return
        return 123;
    }
Copy after login

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]    删除某个键值
    }
Copy after login

3.app_path(), get the app path

//辅助函数
    public function help()
    {
        $data = app_path();
        return $data;
    }
Copy after login

4.base_path(), get the project root directory path

//辅助函数
    public function help()
    {
        $data = base_path();
        return $data;
    }
Copy after login

5 .config_path(), get the config path

//辅助函数
    public function help()
    {
        $data = config_path();
        return $data;
    }
Copy after login

6.database_path(), get the database path

//辅助函数
    public function help()
    {
        $data = database_path();
        return $data;
    }
Copy after login

7.public_path(), get the public path

//辅助函数
    public function help()
    {
        $data = public_path();
        return $data;
    }
Copy after login

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'));    //链式操作,返回字符串之前的字符串
    }
Copy after login

9.action(), generate url

//辅助函数
    public function help()
    {
        $url = action([HomeController::class, 'index']);
        return $url;
    }
Copy after login

10.asset(), and generate url

//辅助函数
    public function help()
    {
        $url = asset('img/abc.jpg');
        return $url;
    }
Copy after login

11.env (), get environment configuration

//辅助函数
    public function help()
    {
        $data = env('APP_ENV');
        return $data;
    }
Copy after login

12.info(), print log

//辅助函数
    public function help()
    {
        $data = info('this is a test log info');    //向storage/logs/laravel.log中插入一条日志
        return $data;
    }
Copy after login

13.redirect(), jump route

//辅助函数
    public function help()
    {
        redirect('/');
    }
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template