一,配置
1,在laravel项目的根目录下添加extend目录,如图:
2,编辑composer.json,在autoload增加一行:
"":"extend/",
如图:
生成自动加载文件:
liuhongdi@lhdpc:/data/laravel/dignews$ composer dump-autoload -o
Generating optimized autoload files
...
命令的解释:
将PSR-0/4自动加载成classmap来获取一个更快速的类加载器,
-o:优化
查看此命令的帮助:
liuhongdi@lhdpc:/data/laravel/dignews$ composer help dump-autoload
如图:
二,php代码:
1,写一个简单的类:extend/arraystr/ArrayStr.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php
namespace arraystr;
/*
* 二维数组转字符串
* */
class ArrayStr {
//构造函数,初始化
function __construct() {
}
//得到二维数组内容
function getArrayContent( $arr ) {
$final_str = "" ;
//遍历
foreach ( $arr as $k => $row ) {
$oneline = "" ;
foreach ( $row as $kk => $value ) {
$oneline = $oneline . $value . " " ;
}
$oneline = $oneline . "\r\n" ;
$final_str .= $oneline ;
}
//返回
return $final_str ;
}
}
|
2,在controller中调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
use arraystr\ArrayStr;
class NewsController extends Controller
{
//得到二维数组内容
public function arrayContent(){
$arr = [
[ 'name' => '老刘' , 'age' => '28' ],
[ 'name' => '老王' , 'age' => '32' ],
];
$arrUtil = new ArrayStr();
$str = $arrUtil ->getArrayContent( $arr );
echo $str ;
}
|
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/07/laravel-zi-dong-jia-zai-zi-ding-yi-lei-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: [email protected]
三,测试效果:
四,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0
标签:laravel,自定义,10.27,liuhongdi,oneline,str,com,加载
From: https://www.cnblogs.com/architectforest/p/17818108.html