是否有一种从一维数组构建链接列表的简单方法?
例如:
$array = [
'slug-1' => 'Title 1',
'slug-2' => 'Title 2',
'slug-3' => 'Title 3'
]呼应as:<a href="slug-1">Title 1</a>, <a href="slug-2">Title 2</a>, <a href="slug-3">Title 3</a>
基本上,一个修改的内爆函数,可以添加额外的参数,如class="something"和类似的。
我怀疑这就是我想要做的。
创建全局功能:
app/Http/helpers.phpcomposer.json块中添加"files": ["app/Http/helpers.php"]编辑autoloadcomposer dump-autoload正如所描述的这里。
不知道怎么从这里开始。我尝试将以下代码添加到helpers.php中
<?php
/**
* Given the array of type ['slug' => 'title', ...]
* create new array of type [ '0' => '<a href="slug">title</a>', ...]
* if $attributes given (also array) as ['id'=>'newLink', 'class'=>'newClass']
* add them to first array to get <a href='slug' id='newLink' class='newClass'>title</a>
*
*
* @param $array, $attributes
* @return array
*/
public function linkifyArray($array, $attributes){
$arrayOfLinks = $array;
return $arrayOfLinks;
}这没有任何作用,但我试图在评论中解释我想要做的事情。
我相信我可以自己创建这个函数,但是当我将上面的代码复制到helpers.php文件时,我得到了内部服务器错误,而PHPStorm告诉我他是expecting statement。
所以我在这里缺少了一些非常基本的东西,在我开始创建我的助手函数之前,我想要一些帮助(链接是最受欢迎的)。
如果我走错路了请告诉我。
最后,我希望能够在我的应用程序中的任何地方执行以下操作:
implode(', ', $array->linkifyArray())从$array获得本问题开头的链接列表。
编辑:
我写了这个函数:
public function linkifyArray($array, $attributes) {
$htmlAttributes = '';
//inline attributes before appending them to <a></a>
if (is_array($attributes))
{
foreach ($attributes as $k => $v)
{
$htmlAttributes.= $k.'="'.$v.'" ';
}
}
$arrayOfLinks = [];
//create array of links
if(is_array($array))
{
foreach ($array as $kk => $vv)
{
$arrayOfLinks[]='<a '.$htmlAttributes.' href="'.$kk.'">'.$vv.'</a>';
}
}
return $arrayOfLinks;
}并尝试将namespace App\Http;添加到<?php下面,但我仍然得到内部服务器错误。
发布于 2015-12-27 21:34:11
我就是这样让它开始工作的。
app/Http/helpers.php
<?php
namespace app\Http;
class Helperfunctions {
/**
* Given the array of type ['slug' => 'title', ...]
* create new array of type [ '0' => '<a href='slug'>title</a>]
* if $attributes given (also array), implode them from ['id'=>'newLink', 'class'=>'newClass']
* to <a href='slug' id='newLink' class='newClass'>title</a>
*
*
* @param $array, $attributes, $prefix
* @return array
*/
public static function linkifyArray($array, $attributes, $prefix) {
$htmlAttributes = '';
//inline attributes before appending them to <a></a>
if (is_array($attributes))
{
foreach ($attributes as $k => $v)
{
$htmlAttributes.= $k.'="'.$v.'" ';
}
}
$arrayOfLinks = [];
//create array of links
if(is_array($array))
{
foreach ($array as $kk => $vv)
{
$arrayOfLinks[]='<a '.$htmlAttributes.' href="'.$prefix.$kk.'">'.$vv.'</a>';
}
}
return $arrayOfLinks;
}
}添加到composer.json autoload>classmap
"autoload": {
"classmap": [
"app/Http/helpers.php"
]
},跑
composer dump-autoload
请按以下方式调用该函数:
{!! implode(', ', \app\Http\Helperfunctions::linkifyArray($myModel()->get()->lists('name', 'slug')->toArray(), [], '/prefix-uri/')) !!}链接到我所用的教程。
发布于 2015-12-28 09:21:35
我见过,你自己想出了解决办法。但是,如果你不想用类来处理这类事情,你可以使用你最初答案的方法。唯一的问题是,在您的helpers.php文件中,您不能使用像public等关键字.
您必须看到这一点,就像一堆没有捆绑在类中的函数一样。在类之外,public关键字会导致错误。
只需在helper.php中以以下方式定义您的函数:
<?php
function linkifyArray($array, $attributes, $prefix) {
// code
}然后,您可以通过键入(从您的回答中获取的示例)在应用程序中访问它。
{!! implode(', ', linkifyArray($myModel()->get()->lists('name', 'slug')->toArray(), [], '/prefix-uri/')) !!}发布于 2015-12-27 20:32:12
public function linkifyArray($array, $attributes){
$arrayOfLinks = $array;
$html = "";
$id = "";
$class = "";
if($attributes['id'])
$id = $attributes['id'];
if($attributes['class'])
$class = $attributes['class'];
foreach($arrayOfLinks as $key=>$val)
{
$html.="<a href='".$key."' id='".$id."' class='".$class."'>".$val."</a>";
}
return $html;
}https://stackoverflow.com/questions/34484464
复制相似问题