Maison  >  Article  >  cadre php  >  Quelles sont les utilisations des fonctions d'assistance de Laravel ?

Quelles sont les utilisations des fonctions d'assistance de Laravel ?

WBOY
WBOYoriginal
2022-02-15 10:49:302619parcourir

Utilisation : 1. La fonction dd() est utilisée pour imprimer les variables données et terminer l'exécution du script. La syntaxe est "dd(variable)" 2. La fonction Asset() est utilisée pour introduire des fichiers statiques. et générer une URL. La syntaxe est "asset (chemin du fichier)" ; 3. La fonction "base_path()" est utilisée pour obtenir le chemin du répertoire racine du projet.

Quelles sont les utilisations des fonctions d'assistance de Laravel ?

L'environnement d'exploitation de ce tutoriel : système Windows 10, version Laravel 6, ordinateur DELL G3.

Quelles sont les utilisations des fonctions auxiliaires de Laravel ?

Quelques fonctions auxiliaires dans Laravel

1.dd(), fonction d'impression

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

2 Opération Array, pour introduire IlluminateSupportArr

//辅助函数
    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() pour obtenir. le chemin de l'application

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

4.base_path(), obtenez le chemin du répertoire racine du projet

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

5.config_path(), obtenez le chemin de configuration

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

6.database_path(), obtenez le chemin de la base de données

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

7.public_path (), obtenez le chemin public

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

8 Opération de chaîne Str, pour introduire IlluminateSupportStr

//辅助函数
    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(), générez l'url

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

10.asset(), générez l'url

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

11.env() et obtenir la configuration de l'environnement

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

12 .info(), imprimer le journal

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

13.redirect(), sauter la route

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

[Recommandations associées : tutoriel vidéo laravel]

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn