3

I have found a few guides like this http://www.mackhankins.com/blog/defining-your-own-helper-classes-in-laravel-4 but it isnt really what im looking for as i want to be able to use functions to modify values that arent members of a class.

I have a lot of regex functions that will modify strings if they match a pattern and return a modified string. I want to be able to call these functions as if they were built in php functions. is what i would have always done in the past, but i want to do this right.

This post What are the best practices and best places for laravel 4 helpers or basic functions? seems to be getting closer, but im still not clear on why i would have to instantiate an object just to run a basic function.

Community
  • 1
  • 1
Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
  • regardless of do-ability, sounds like a terrible idea going against all sorts of best practice –  Mar 05 '15 at 20:09
  • What about it is bad practice? There is nothing wrong with functions. In fact, Laravel 5 makes heavy use of functions. `view();` `redirect()`, etc. – kylehyde215 Mar 05 '15 at 20:10
  • instead of putting bunch of unrelated functions in the same file making global.... use facade?????? – itachi Mar 06 '15 at 03:35
  • @itachi Seems overkill. I don't see `UtilClass::someRegExpMethod()` being any better then `someRegExpFunction()` or `MyNamespace\Util\someRegExpFunction()` – kylehyde215 Mar 06 '15 at 06:04

1 Answers1

2

Just plop all your custom functions in a file names something like "helpers.php". You don't need to do anything special.

You could either directly include the file in your bootstrap file or just set composer up to auto-load it.

kylehyde215
  • 1,216
  • 1
  • 11
  • 18
  • From what ive read this is considered bad practice?? Is it ok to go ahead and do this anyway? – Dan Hastings Mar 05 '15 at 20:18
  • @DanHastings I don't see anything wrong with it. Not much difference between a global function and a class with a bunch of static methods. You can also namespace your functions just as like with classes if you're concerned with naming collisions. – kylehyde215 Mar 05 '15 at 20:20
  • ive always done it this way so it makes sense to me to it like this. I just second guess everything ever since i started working with laravel since everything i once knew is now wrong :P anyway this does exactly what i wanted. thanks – Dan Hastings Mar 05 '15 at 20:25