我在我的应用程序中添加了services文件夹。我在composer.json中添加了app / models下面的apps / services。
然后我执行了“ composer dump-autoload”
这是我的控制器:
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);
}
这是我的服务
ShiftService.php
namespace App\Services;
//use App\Models\Shift;
class ShiftService {
public static function retrieveShift($request){
$shift=Shift::where("shift","=",$request)->first();
return $shift;
}
}
我将名称空间App放在服务上还是注释掉并替换它都没有关系。当我使用Use App \ Services或Use \ App \ Services或Use \ App \ Services \ ShiftService时,它也无法正常运行,
如果我在LogbookController.php中添加了名称空间App或名称空间App \ Controllers,则不会检测到控制器。我很困惑该怎么办。
是配置吗?我想念什么吗?作曲家和设置有问题吗?我目前正在使用PHP 5.4,但我的Laravel也是较旧的版本。
我的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"
}
答案 0 :(得分:0)
我解决了,
首先,第二天我进入作曲家dump-autoload时,由于某种原因,它起作用了。我认为这需要硬重置。
然后我将其添加到LogbookController.php的标题中:
use App\Services;
use App\Models;
class LogbookController extends \BaseController {
...
}
然后我在TransactionService.php上添加了它
namespace App\Services;
use App\Models;
use App\Services\TransactionService;
class TransactionService {
...
}
基本上,名称空间和use语句很棘手,因此我不得不进行测试和重新测试 使用它。