Viewed   75 times

I would like to create helper functions to avoid repeating code between views in Laravel 5:

view.blade.php

<p>Foo Formated text: {{ fooFormatText($text) }}</p>

They're basically text formatting functions. Where and how can I create a file with these functions?

 Answers

4

Create a helpers.php file in your app folder and load it up with composer:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

After adding that to your composer.json file, run the following command:

composer dump-autoload

If you don't like keeping your helpers.php file in your app directory (because it's not a PSR-4 namespaced class file), you can do what the laravel.com website does: store the helpers.php in the bootstrap directory. Remember to set it in your composer.json file:

"files": [
    "bootstrap/helpers.php"
]

Tip: If you want to use the different file name instead of helpers, you can change the file name and path. Also, you can create multiple helper files. It will look like this:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files": [
        "app/Helpers/base.php", // <---- ADD THIS
        "app/Helpers/metrics.php" // <---- Create `metrics.php` file in this path and add the path in composer.json file.
    ]
},
Thursday, August 11, 2022
2

The "illuminate/html" component is no more supported by Laravel and is not compatible with the 5.2 version.

You can replace it with laravelcollective/html

You have to:

  • remove the reference of "illuminate/html": "^5.0@dev", from composer.json

  • add "laravelcollective/html": "5.1.*" (or the version you want)

  • run composer update to update the dependecies (this will remove your "illuminate/html" component and install the "laravelcollective/html" component

Now you have to replace your HTML service provider with:

 'providers' => [
    CollectiveHtmlHtmlServiceProvider::class,
  ],

and the facades:

  'aliases' => [
      'Form' => CollectiveHtmlFormFacade::class,
      'Html' => CollectiveHtmlHtmlFacade::class,
  ],
Wednesday, December 21, 2022
 
showdev
 
3

If you want to go the 'Laravel way', you can create helpers.php file with custom helpers:

if (! function_exists('myCustomHelper')) {
    function myCustomHelper()
    {
        return 'Hey, it's working!';
    }
}

Then put this file in some directory, add this directory to autoload section of an app's composer.json:

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

Run composer dumpauto command and your helpers will work through all the app, like Laravel ones.

If you want more examples, look at original Laravel helpers at /vendor/laravel/framework/Illuminate/Support/helpers.php

Wednesday, September 7, 2022
 
wolfcow
 
5

Try the following:

  1. Make a bind class where you can implement each rule you want extending Validator class.
  2. Make a service provider that extends ServiceProvider.
  3. Add your custom validator provider at config/app.php file.

You can create the bind at Services folder like this:

namespace MyAppServices;

class Validator extends IlluminateValidationValidator{

    public function validateFoo($attribute, $value, $parameters){  
        return $value == "foo"
    }
}

Then, use a service provider to extends the core:

namespace MyAppProviders;

use MyAppServicesValidator;
use IlluminateSupportServiceProvider;

class ValidatorServiceProvider extends ServiceProvider{

    public function boot()
    {
        Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new Validator($translator, $data, $rules, $messages);
        });
    }

    public function register()
    {
    }
}

Finally, import your service provider at config/app.php like so:

'providers' => [
    ...
    ...
    'MyAppProvidersValidatorServiceProvider';
]
Thursday, December 22, 2022
 
1

For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

Create a new file in the config directory. Let's call it constants.php

In there you have to return an array of config values.

return [
    'options' => [
        'option_attachment' => '13',
        'option_email' => '14',
        'option_monetery' => '15',
        'option_ratings' => '16',
        'option_textarea' => '17',
    ]
];

And you can access them as follows

Config::get('constants.options');
// or if you want a specific one
Config::get('constants.options.option_attachment');
Friday, December 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :