I added the services folder in my app. I added in composer.json the apps/services below apps/models.
Then I performed "composer dump-autoload"
Here's my Controller:
LogbookController.php
namespace App
use App\Services;
class LogbookController extends \BaseController {
public function index()
{
return Response::json($this->retrieveCashLogbook('977'));
}
public function retrieveLogbookHeader($logbook){
$log_id=$logbook->id;
$logbook->log_shift=\App\Services\ShiftService::retrieveShift($log_id);
return $logbook;
}
public function retrieveCashLogbook($id){
$logbook=Logbook::find($id);
$logbook=$this->retrieveLogbookHeader($logbook);
}
Here's my Service:
ShiftService.php
namespace App\Services;
//use App\Models\Shift;
class ShiftService {
public static function retrieveShift($request){
$shift=Shift::where("shift","=",$request)->first();
return $shift;
}
}
It didn't matter if I put namespace App on the Service or I comment it out and replace it. It also didn't work when I used Use App\Services or Use\App\Services or Use \App\Services\ShiftService, it still doesn't get detected.
If I added namespace App or namespace App\Controllers on my LogbookController.php, the controller isn't detected. I'm confused what to do.
Is it the configuration? Am I missing something? Is there something wrong with composer and the setup? I'm currently using PHP 5.4 but my Laravel is also an older version.
My composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.1.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/services",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
I solved it,
First, when I entered composer dump-autoload the next day, it worked for some reason. I think it needed a hard reset.
Then I added this on the header of LogbookController.php:
use App\Services;
use App\Models;
class LogbookController extends \BaseController {
...
}
Then I added this on TransactionService.php
namespace App\Services;
use App\Models;
use App\Services\TransactionService;
class TransactionService {
...
}
Basically, the namespace and the use statements were tricky, so I had to test and retest using it.