code-8
code-8

Reputation: 58830

Passing a function from controller to view

I have a function that take in a string, and convert it to a Mac Address format.

    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 ?

Upvotes: 1

Views: 2043

Answers (5)

David Vielhuber
David Vielhuber

Reputation: 3569

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') }}

Upvotes: 0

Denis
Denis

Reputation: 410

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)
    {
        ...
    }
}

Upvotes: 1

Steve Bauman
Steve Bauman

Reputation: 8688

Another clean way of doing this is through extending blade (utilizing @dtj's answer):

Create a helpers.php file:

// 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:

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

Run composer dump-autoload

In 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.

Upvotes: 1

djt
djt

Reputation: 7535

This may be a good candidate for a helper, depending on how much you really need the functionality throughout your site.

create a helpers.php file

<?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

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

then run composer dump-autoload in your terminal, and your helpers methods will be available throughout your application. Doing it this way, you can add more helper functions to helpers.php as needed.

Upvotes: 7

nestedl00p
nestedl00p

Reputation: 490

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

Also you can use laravels session library Laravel Session

Upvotes: 2

Related Questions