Chào mọi người,
Mình đang tìm hiểu về Laravel 5, và đang gặp vấn đề như sau:
Mình có class PageService
:
<?php
namespace App\Services;
class PageService {
public function publishPost() {
echo 'Post published!';
}
public function updatePost() {
echo 'Post updated!';
}
public function removePost() {
echo 'Post removed!';
}
}
Và TestController
:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\PageService;
class TestController extends Controller
{
public function index(PageService $pageService)
{
$pageService->publishPost();
}
}
File web.php
:
Route::get('/test', TestController@index);
Khi mình truy cập localhost/test
thì vẫn in ra được kết quả là ‘Post published!’ chứng tỏ Laravel đã tự khởi tạo 1 instance của PageService
và inject vào function index
trong khi mình chưa register PageService
vào Service Provider
, nếu việc này là bình thường thì cho mình hỏi mục đích chính xác của việc register Service vào Service Provider
là gì vậy ạ?