Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在研究 DBAL symfony2。我以这种方式使用 DBAL 建立了数据库连接 public function demoAction() { $conn = $this->get('database_connection'); 我被卡住了@有什么方法可以声明全局对象/常量以进行连接,而不是在每个动作上调用它。
谢谢
全局对象不是 symfony2 中的最佳实践。你可以做的是创建一个 BaseController,并让你的所有控制器扩展这个基本控制器:
class BaseController extends Controller { public function getDBAL() { return $this->get('database_connection'); } }
请注意,在 Controller 中使用 DBAL 仍然是一种不好的做法。您应该为要管理的实体声明服务。