Laravel Custom Global Function Example

Jun 15, 2021 . Admin

Hello Friends,

Now let's see example of how to create custom global function in laravel example. This is a short guide on laravel custom global function. Here you will learn how to create custom global function. We will use how to create custom global function in laravel. Let's get started with how to create custom global function in laravel.

Here i will give you few step instruction to create custom global function in laravel.

Step 1: create a helper file

Go to app\ directory and create a file called helpers.php

Path: app\Http\helpers.php

Step 2: Write the helper function

Path: app\Http\helpers.php

<?php
	 /**
	 * Write code on Method
	 *
	 * @return response()
	 */
	function getImageValue()
	{
		return 'https://www.mywebtuts.com/upload/blog/laravel-custom-global-func-example.png';
	}
?>
Step 3: Include it in our composer.json

we need to include the helpers.php file in our composer.json, so that when we autoload, it will load the file.

Path: composer.json

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
         "files": [
            "app/Http/helpers.php"
        ]
    },
Step 4: Regenerate the list of all classes in the app
composer dump-autoload

We can now call our function anywhere in our app

Step 5: View File
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>Laravel Custom Global Function Example</title>
	</head>
	<body>
		<img src="{{ getImageValue()}}">
	</body>
</html>

It will help you....

#Laravel