Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mrs_Beginner's avatar

How many way exist to add custom classes to laravel 5.* ?

i want to send some data with $curl and use them in all Controllers that needed.

but i want to know when add class with facades or use Service Container or add them as file in app folder. which one is better for me now? which one has better performance? and is there any more ways? and what is differences between this ways?

0 likes
6 replies
Sergiu17's avatar

Could you be more specific? The difference between which classes? Between a class created with File -> New Class and other created with CTRL+N ( for new file ) ? I don't get your question!

1 like
Mrs_Beginner's avatar

@Sergiu17

i mean like with facades or without it or other ways if exist,

sorry for bad question, question edited.

36864's avatar

Still unclear on what you're asking.

What does "add custom classes to laravel" mean to you?

You can use any class you define in your project. Laravel is still just php, so even if you don't do the whole autoload thing you can still just import php files and use whatever classes are defined there.

It would be easier to help if we knew exactly what you're looking for.

1 like
Mrs_Beginner's avatar

@36864 @Sergiu17

i want to send some data with $curl and use them in all Controllers that needed.

but i want to know when add class with facades or use Service Container or add them as file in app folder. which one is better for me now? which one has better performance? and is there any more ways? and what is differences between this ways?

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@Mrs_Beginner Facade is a Design Pattern, read more about it on Wiki https://en.wikipedia.org/wiki/Facade_pattern

But a Class is just a Class, nothing more than this. Now, depending on what class is doing, you could classify them.

  • Classes inside Controllers - well, this classes are called controllers, they are responsible for request / response
  • Classes inside Services - this is a Service class, this class may provide functionality like Uploading Files or Subscribe Users to Newsletters
  • Classes inside Repositories - just a simple Repository class in this class goes complex queries. You don't want 10 lines inside your controller which fetches all products with description, tags, user, images, sorting by price and so on, you just have an ProductsRepository.php
<?php

use App\Models\Product;

class ProductRepository {
    public function findAllProductsSortingByPrice($price)
    {
        // queries may go very complex
        Product::with(['images', 'user', 'tags'])->orderBy()->whereHas()->get()
    }
}

There are a lot of examples

You want to send or to fetch data? No matter what, you have to create a class called CurlService.php inside app\Services\CurlService.phpand add function which do your work.

Inside your controller, inject the service in constructor and you are free to use the service, example:

class YourControllerClass extends Controller
{
    protected $curl;

    public function __construct(CurlService $curlService)
    {
        $this->curl = $curlService;
    }

    public function index()
    {
        $data = SomeData;

        $this->curl->send($data);
    }
}
2 likes
lara56278's avatar

@Mrs_Beginner

One of the best parts about development is that you as the developer get to decide what which tool is the best tool for the job depending on what you are trying to do. This applies to the creation of any file and any class and of course considerations must be given to how complex or simple the project you are working on is.

@Sergiu17 gave a great answer regarding classes and if you look into the depths of Laravel, it consists of many, many classes that all do different things based on the context they are in.

When deciding on making a class, consider it's context and it's use; then decide where the appropriate place is to create the class.

1 like

Please or to participate in this conversation.