Create new classes in laravel 5.2
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Reusability: How to Create Custom Classes in Laravel
As a seasoned developer working within the Laravel ecosystem, you constantly encounter the need for reusable logic. Just as Laravel provides elegant built-in classes like `Request` and `Redirect` to handle common tasks, developers often find themselves needing custom components—classes that encapsulate specific business rules or actions—that can be utilized across multiple controllers without duplication.
The scenario you presented—creating classes like `MyNewClass` and making them accessible globally within your application—touches upon the core principles of Object-Oriented Programming (OOP) and how Laravel structures its dependency management. While PHP itself is the foundation, Laravel provides specific patterns to make these custom creations truly seamless and scalable.
This post will walk you through the developer-centric ways to achieve this reusability in a modern Laravel project, moving beyond simple class definition to true application architecture.
## The Foundation: Organizing Your Application Logic
The first step when creating any custom class is proper file structure. Laravel relies heavily on PSR-4 autoloading to map namespaces to directories automatically. To make your classes easily accessible and maintainable, you should place them within the appropriate directory structure, typically under the `app/` namespace.
For a custom class named `MyNewClass`, you would create a dedicated directory:
```
app/
└── Classes/
└── MyNewClass.php
```
Inside this file, you define your class adhering to standard PHP conventions:
```php
// app/Classes/MyNewClass.php
namespace App\Classes;
class MyNewClass
{
public function createSomething(MySecondNewClass $secondClass)
{
// Your reusable business logic goes here
$result = "Action performed successfully.";
return $result;
}
}
```
This separation keeps your core application files clean. When you reference this class in a controller, you use the fully qualified namespace: `\App\Classes\MyNewClass`.
## Implementing Reusability with Service Classes
Simply creating a class is not enough to make it behave like Laravel's built-in components. To truly leverage Laravel’s structure for dependency management and reusability, we should treat these custom classes as **Service Classes** or **Action Classes**. These classes should focus purely on the *what* (the business logic) rather than the *how* (the HTTP request handling).
Instead of trying to inject arbitrary variables directly into controllers, you should use Laravel's powerful **Service Container** via **Service Providers**. This is how you register your custom services so they can be resolved and injected wherever needed. This approach aligns perfectly with the principles demonstrated by high-quality Laravel code found on resources like https://laravelcompany.com.
### Example: Using a Service Class in a Controller
Let’s refactor your controller to utilize this new service class, demonstrating how you achieve dependency injection rather than manual instantiation within the method body.
First, ensure your custom class is registered (usually done within a Service Provider). Then, in your controller, you inject the necessary dependencies into the constructor or method signature:
```php
// app/Http/Controllers/ArticleController.php
namespace App\Http\Controllers;
use App\Classes\MyNewClass; // Import your custom class
class ArticleController extends Controller
{
protected $myNewClass;
// Dependency Injection via the constructor (Best Practice)
public function __construct(MyNewClass $myNewClass)
{
$this->myNewClass = $myNewClass;
}
public function bla()
{
// Now we use the injected service, making the controller cleaner
$secondClassInstance = new \App\Classes\MySecondNewClass(); // Can instantiate if necessary
$result = $this->myNewClass->createSomething($secondClassInstance);
return response()->json(['message' => $result]);
}
}
```
## Conclusion: The Laravel Philosophy of Reusability
The key takeaway is that in Laravel, reusability isn't achieved by forcing classes into the controller methods; it’s achieved by leveraging dependency injection and the Service Container. By defining your custom logic as dedicated service classes and registering them correctly, you ensure your code remains decoupled, testable, and adheres to the elegant architecture promoted by the framework.
For deeper dives into structuring services within Laravel, always refer back to official documentation and best practices from https://laravelcompany.com to ensure your custom components integrate seamlessly with the rest of the ecosystem. By adopting this approach, you move from writing isolated scripts to building robust, enterprise-grade applications.