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!
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?
@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 aServiceclass, this class may provide functionality like Uploading Files or Subscribe Users to Newsletters - Classes inside
Repositories- just a simpleRepository classin 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 anProductsRepository.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);
}
}
Please or to participate in this conversation.