문제

How can I create helpers in Laravel and where should I put them? I was thinking on something like codeigniter's approach: a helpers.php file and some helper functions in there where I can call as needed.

There is not much about the subject in the documentation.

도움이 되었습니까?

해결책 2

I really depends on what you're doing, but in Laravel 3 your goal should be to extract as many reusable portions of code into bundles. Take a look at the existing bundles and you might find something which will suit your needs built already (or at least good examples).

For "helpers" your still best off creating classes and defining the helper functions as static methods, this is clean and typically easy to follow.

If you don't want to create a bundle for the helpers, then you can also place them within the application/models folder, again wrapped in a class.

Here is a thread from the forum which shows some examples of "helper" type classes.

다른 팁

You can indeed create your own helpers file.

You should require it in your applications start.php.

Create a class in libraries directory, you can add few static methods inside the class and you can use it like Common::method(), but if you want to use just function() then put your functions out of your class statement.

Example:

class Common
{

}

function display_messages()
{
  exit('Yes');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top