我可能會像這樣的東西去:
class BaseController {
$user = null;
public function __contruct() {
$this->user = someFunctionToGetUser();
}
}
class Controller1 extends BaseController {
public function someFunction1() {
// $this->user is set and accessible here
}
}
class Controller2 extends BaseController {
public function someFunction2() {
// $this->user is set and accessible here too
}
}
class Controller3 extends BaseController {
public function someFunction3() {
// $this->user is set and accessible here too also
}
}
很顯然,我沒有對自己目前的架構或當前代碼的想法,但你的想法。
編輯:
更新關於Silex要求。
其實我有寫在Silex的一個API。該應用程序的前端控制器位於/web/index.php中。 index.php文件看起來是這樣的:
$app = require __DIR__.'/../bootstrap.php';
$app->run();
bootstrap.php中看起來是這樣的:
require __DIR__ . '/vendor/autoload.php';
$app = new Silex\Application();
require APP_ROUTES . 'Ranges.php';
return $app;
併成立Ranges.php最多的例子是:
$app->get('/ranges', function() use ($app) {
// some code to generate results
return $app->json($results);
}
您可以輕鬆地設置您的課程將使用$ app(the silex實例),與我爲路線所做的相同。
我想親自與父抽象基本控制器一起 - 似乎是最適合我的想法。 – CD001