D
D
daMage2015-04-11 11:15:39
Laravel
daMage, 2015-04-11 11:15:39

How to create a helper using Laravel as an example?

Hello. Help create view, db helpers like in Laravel. Now I just declared a global function

function view($template) {
  return new View($template);
}

I'm going to tie DI to the application, so this method is not suitable. Also, I can't create a log helper because the function is already there by default in php, it turns out you need a different namespace. If the helper needs a dependency, then you need to create a class with the necessary methods. This will solve both problems, but calling view, for example, will no longer work. Is it possible to somehow "unload" class methods in order to use nice names in the view and model?
PS: I don't want to use $this->view.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2015-04-11
@daMage

Judging by the description, you do not need to write a helper, but create some kind of service.
In Laravel, for example, they write ServiceProvider for this.
If we talk specifically about the view, then you need to define the composer

O
OnYourLips, 2015-04-11
@OnYourLips

Create app/helpers.php, register it in autoload in composer, make the necessary functions in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question