How create own class helper in Laravel? - laravel-5

I need to create own class with methods, that I want to call from diverse controllers.
What it should be: Library, Provider or Helper in Laravel?

1: Create folder app/Helpers
2: In app/Providers create new provider file HelperServiceProvider.php
3: In this file register all helpers classes you need
$this->app->bind('dateHelper', function()
{
return new \App\Helpers\DateHelper;
});
... etc
4: In config/app.php add this new provider
'App\Providers\HelperServiceProvider',
5: Then you need to create Facade to be available to use this helper in view. You find the info about how to create Facade on official laravel.com site
About the providers, you can read the doc
Source: Laravel Forums

You can do this in many ways
Static way
Create a folder your wish Utils or Helpers or Libraries.
Create a Class (ex: Helper Class)
Added static methods here (ex: public static common()).
Added namespace to the call.
Use the name space and call the static function using(Helper::common)
Normal Class
Create class with name space.
Inject the Dependency of that class and use all function inside.

You can follow the simple steps below
Step 1
Create new helper file in app/Helpers directory
Ex. I have created the DemoHelper.php in directory app/Helpers/DemoHelper.php
Step 2
Add the entry of created Helper(DemoHelper.php) to composer.json file in autoload section
"autoload": {
"files": [
"app/Helpers/Helper.php",
"app/Helpers/DemoHelper.php"
]
},
Step 3
Finaly, composer dump-autoload hit this command in terminal.

Related

Using different namespaces models for Resources in Laravel 5.5

love the Resources function in Laravel 5.5 which creates at resource prettier for a Model, but I have all my models stored in /App/Models/* instead of directly in the App/*-folder.
This is causing the App/Http/Resources/* not to work.
Results in a "Undefined property: Illuminate\Database\Query\Builder::$map"
This is caused because I stored my Models in a different folder which he can't map to by guessing.
Where and how to define the different placing of the Model?
For what I understand of your problem you have to inject the Model instance in the resource constructor, there is no magical autobinding, i.e.:
use App\Models\User;
use App\Http\Resources\User as UserResource;
Route::get('/user', function () {
return new UserResource(User::find(1));
});
I dont see any problem here, even with namespaced Models.
Not sure of the process you used to move the models to the new Models directory, so check a few things:
Namespace of the models
Each model has:
namespace App\Models;
Check references in these files
app/Http/Controllers/Auth/RegisterController.php
config/auth.php
config/services.php
database/factories/ModelFactory.php
Your Controllers
And change App/ModelExample to App/Models/ModelExample
Update autoload classmap
Add "app/models" to composer.json's classmap autoload section
"autoload": {
"classmap": [
"database",
"app/models"
]
}
Autoload files
Run composer dump-autoload

How to access a package's model?

I have a package that implements tagging functionality. In order to create new tags, I need to access the model inside the vendor folder. However, I get an error when I try the following:
$tag = new vendor\codecourse\taggy\src\models\tag;
Class 'vendor\codecourse\taggy\src\models\tag' not found
How do I access this model?
vendor is assumed. Look at the namespace declaration in the file you want to use to figure out the rest. Per https://github.com/codecourse/taggy/blob/master/src/Models/Tag.php, it'll be:
$tag = new Codecourse\Taggy\Models\Tag;

How to autoload multiple classes in Laravel 4 application?

I’ve created a workbench package in Laravel 4, which is name-spaced and has two directories: Models and Contexts. Somehow, Laravel is loading my models in my Models directory (I have a model in there called User), however, it doesn’t know about my classes in the Contexts directory.
I want to be able to use my context classes in my app’s controllers without specifying the whole namespace, so I thought I’d add them to Laravel’s IoC container. However, it seems I need to create a façade class for each class I wish to add to the container. This isn’t desirable if I have dozens of context classes, as it would mean creating an equal amount of façade classes too.
Is there a way in Laravel to bulk-add classes to its IoC container?
if you want to use one term facades for your classes the laravel way (e.g. MyModel::someAction()) then you have to create your facades. but i'd advise not to do so for so many classes.
if your classes inside contexts folder aren't found then you should check your composer.json file under the autoload entry or do a composer dump-autoload -o.
I'd just DI the classes within the constructor of the class that uses them, so you end up using $this->myService->someAction().
This should answer both Laravel 4 and 5.
First, you need to use the bind method Illuminate\Foundation\Application class, which serves to register binding in the service container. In the Laravel documentation you will find plenty of examples how to do that, but only for a single binding.
If you take a look a the implementation of bind method here or just the definition here, you will notice that this method accepts a string|array. This means you can provide multiple bindings as an array and register all of them in the container with their fully qualified class names. Something like this:
$this->app->bind(['\App\Acme\Service1', '\App\Acme\Service2', '\App\Acme\Service3', ...]
Having this in mind, you can easily get the classes in one namespace (directory) with a reflection, put them in array and use the above method to register them.
Revisiting this question some time later, I think the appropriate solution would be to autoload the classes using my package’s composer.json file, and then import classes using it’s FQN in controllers and other classes:
<?php
use Vendor\Package\Contexts\ContextClass;
class Laravel4Controller extends BaseController {
protected $context;
public function __construct(ContextClass $context) {
$this->context = $context;
}
}

Laravel model containing multiple classes

I'm new to laravel, coming from Codeigntier. I am trying to get to grips with the model/classes/eloquent convention that Laravel uses. I know that I can make a table in my database called, say, "categories". Then I can make a file in my models folder called category.php containing just the following code:
Class Category extends Eloquent { }
which automatically connects with the table name with the plural version of the name ("categories") and gives me access to all the Eloquent commands in my controller like Category::All();
Here's what I don't get: Do I need to make a new model for every table in my database? In that case I will end up with a bunch of files in my models folder with names like resource1.php, resource2.php, etc, each just containing the same code as above (replacing the name of the class).
Can't I just make one model called, say, "database_handler.php" and then just put all these classes into it in the same place?
Yes, you can create a database_handler.php file and do:
<?php
Class Category extends Eloquent { }
Class Post extends Eloquent { }
Class Client extends Eloquent { }
You can do whatever PHP let's you do, and add many classes to a single .php file is something you can do. But this is not a good practice and Laravel is all about developing application using the best ones.
To load this file automatically, you can do one of many things:
1) Add this file to your app/models folder.
2) Add an entry for it on your composer.json:
"autoload": {
"files": [
"app/database_handler.php"
]
},
And then you'll have to
composer dump-autoload -o
3) Create a different folder for it and also add it to composer json autoload section.
You choose, Laravel will let you free to do whatever you want.

Magento - create a helper class

I'm having trouble figuring out how to create a helper class with a function in it that's available to *.phtml files.
Can someone describe step by step how I can make the function prtHelloWorld() available to all my *.phtml files?
it's rather simple and you have to call your helper from template like this:
Mage::helper('yourmodule/yourclassfile')->prtHelloWorld();
Default helper class is Data and this defaults to Yourmodule/Helper/Data.php
Mage::helper('yourmodule')->prtHelloWorld();
To add to Anton S's, if you want to be able to access the helper's function using $this->prtHelloWorld() in the phtml instead, add it to your Block like this:
public function prtHelloWorld() {
return Mage::helper(whatever)->prtHelloWorld();
}

Resources