简体   繁体   English

将函数从控制器传递到视图

[英]Passing a function from controller to view

I have a function that take in a string, and convert it to a Mac Address format.我有一个接收字符串的函数,并将其转换为 Mac 地址格式。

    function mac_adress($str){
        $end = substr($str, -12);
        $chunks = str_split($end, 2);
        $result = implode(':', $chunks);
        return $result;
    }

I want pass function to my view so I can just call it like this我想将函数传递给我的视图,这样我就可以像这样调用它

$str = 836997595163;

{!!mac_adress($str)!!} //83:69:97:59:51:63

I have multiple views that need this functionally, and I'm trying to avoid place them in every single one of my blade file.我有多个视图在功能上需要这个,我试图避免将它们放在我的每个刀片文件中。

How can I do that ?我怎样才能做到这一点 ?

Is it even possible ?甚至有可能吗?

This may be a good candidate for a helper , depending on how much you really need the functionality throughout your site. 这可能是一个不错的helper ,具体取决于您在整个站点中实际需要多少功能。

create a helpers.php file 创建一个helpers.php文件

<?php

function mac_adress($str){
     $end = substr($str, -12);
     $chunks = str_split($end, 2);
     $result = implode(':', $chunks);
     return $result;
 }

And then autoload it in composer.json 然后将其自动加载到composer.json

{
    "autoload": {
        "files": [
            "app/helpers.php"
        ]
    }
}

then run composer dump-autoload in your terminal, and your helpers methods will be available throughout your application. 然后在终端中运行composer dump-autoload ,您的助手方法将在整个应用程序中可用。 Doing it this way, you can add more helper functions to helpers.php as needed. 这样,您可以根据需要向helpers.php添加更多的辅助函数。

You can set it as a session variable, and call it as many times and many places needed 您可以将其设置为会话变量,并根据需要多次调用它

$_SESSION['mac-address'] = mac_address($str);

More information here $_SESSION 更多信息在这里$ _SESSION

Also you can use laravels session library Laravel Session 您也可以使用laravels会话库Laravel会话

Export it to a service class from where you can call it easily from every view 从每个视图轻松将其导出到服务类

<?php $formatedMacAddress = \App\Services\MacAddressHelper::formatMacAdress($macAddress);?>


<div>{{ $formatedMacAddress }}</div>
namespace \App\Services;

class MacAddressHelper
{
    public static function formatMacAdress(string $macAddress)
    {
        ...
    }
}

Another clean way of doing this is through extending blade (utilizing @dtj's answer): 另一种干净的方法是通过扩展刀片(利用@dtj的答案):

Create a helpers.php file: 创建一个helpers.php文件:

// app/helpers.php

function macAddress($str) {
    $end = substr($str, -12);

    $chunks = str_split($end, 2);

    $result = implode(':', $chunks);

    return $result;
}

Edit your composer.json file: 编辑您的composer.json文件:

{
    "autoload": {
        "files": [
            "app/helpers.php"
        ]
    }
}

Run composer dump-autoload 运行composer dump-autoload

In AppServiceProvider: 在AppServiceProvider中:

// app/Providers/AppServiceProvider.php

public function boot()
{
    Blade::directive('mac', function($expression) {
        return "<?php echo macAddress{$expression}; ?>";
    });
}

Then in your views: 然后在您看来:

@mac('836997595163')

This way you can modify the helper function at any time and have a single reference to it. 这样,您可以随时修改助手功能并对其进行单个引用。

If anybody is interested in a solution how to pass a function to a view (which is only used inside this view):如果有人对如何将函数传递给视图(仅在此视图中使用)的解决方案感兴趣:

In your controller:在您的控制器中:

/* ... */
public function getTestOverview()
   return view('test', [
      'mac_adress' => function($str) { return $this->mac_adress($str); }
   ]);
}

private function mac_adress($str){
   $end = substr($str, -12);
   $chunks = str_split($end, 2);
   $result = implode(':', $chunks);
   return $result;
}
/* ... */

In your view:在您看来:

{{ $mac_adress('foo') }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM