# 使用命名空間(Working with Namespaces)
[Namespaces](http://php.net/manual/en/language.namespaces.php)可以用來避免類名的沖突,比如如果在一個應用中有兩個控制器使用同樣的名稱,那么可以用namespace來區分他們。 另外命名空間在創建組件或者模塊的時候也是非常有用的。
## 設置框架(Setting up the framework)
Using namespaces has some implications when loading the appropriate controller. To adjust the framework behavior to namespaces is necessary to perform one or all of the following tasks:
Use an autoload strategy that takes into account the namespaces, for example with[Phalcon\\Loader](http://docs.iphalcon.cn/api/Phalcon_Loader.html):
~~~
<?php
$loader->registerNamespaces(
[
"Store\\Admin\\Controllers" => "../bundles/admin/controllers/",
"Store\\Admin\\Models" => "../bundles/admin/models/",
]
);
~~~
Specify it in the routes as a separate parameter in the route’s paths:
~~~
<?php
$router->add(
"/admin/users/my-profile",
[
"namespace" => "Store\\Admin",
"controller" => "Users",
"action" => "profile",
]
);
~~~
Passing it as part of the route:
~~~
<?php
$router->add(
"/:namespace/admin/users/my-profile",
[
"namespace" => 1,
"controller" => "Users",
"action" => "profile",
]
);
~~~
If you are only working with the same namespace for every controller in your application, then you can define a default namespace in the Dispatcher, by doing this, you don’t need to specify a full class name in the router path:
~~~
<?php
use Phalcon\Mvc\Dispatcher;
// Registering a dispatcher
$di->set(
"dispatcher",
function () {
$dispatcher = new Dispatcher();
$dispatcher->setDefaultNamespace(
"Store\\Admin\\Controllers"
);
return $dispatcher;
}
);
~~~
## 控制器加入命名空間(Controllers in Namespaces)
The following example shows how to implement a controller that use namespaces:
~~~
<?php
namespace Store\Admin\Controllers;
use Phalcon\Mvc\Controller;
class UsersController extends Controller
{
public function indexAction()
{
}
public function profileAction()
{
}
}
~~~
## 模型加入命名空間(Models in Namespaces)
Take the following into consideration when using models in namespaces:
~~~
<?php
namespace Store\Models;
use Phalcon\Mvc\Model;
class Robots extends Model
{
}
~~~
If models have relationships they must include the namespace too:
~~~
<?php
namespace Store\Models;
use Phalcon\Mvc\Model;
class Robots extends Model
{
public function initialize()
{
$this->hasMany(
"id",
"Store\\Models\\Parts",
"robots_id",
[
"alias" => "parts",
]
);
}
}
~~~
In PHQL you must write the statements including namespaces:
~~~
<?php
$phql = "SELECT r.* FROM Store\Models\Robots r JOIN Store\Models\Parts p";
~~~
- 簡介
- 安裝
- 安裝(installlation)
- XAMPP下的安裝
- WAMP下安裝
- Nginx安裝說明
- Apache安裝說明
- Cherokee 安裝說明
- 使用 PHP 內置 web 服務器
- Phalcon 開發工具
- Linux 系統下使用 Phalcon 開發工具
- Mac OS X 系統下使用 Phalcon 開發工具
- Windows 系統下使用 Phalcon 開發工具
- 教程
- 教程 1:讓我們通過例子來學習
- 教程 2:INVO簡介
- 教程 3: 保護INVO
- 教程4: 使用CRUD
- 教程5: 定制INVO
- 教程 6: V?kuró
- 教程 7:創建簡單的 REST API
- 組件
- 依賴注入與服務定位器
- MVC架構
- 使用控制器
- 使用模型
- 模型關系
- 事件與事件管理器
- Behaviors
- 模型元數據
- 事務管理
- 驗證數據完整性
- Workingwith Models
- Phalcon查詢語言
- 緩存對象關系映射
- 對象文檔映射 ODM
- 使用視圖
- 視圖助手
- 資源文件管理
- Volt 模版引擎
- MVC 應用
- 路由
- 調度控制器
- Micro Applications
- 使用命名空間
- 事件管理器
- Request Environmen
- 返回響應
- Cookie 管理
- 生成 URL 和 路徑
- 閃存消息
- 使用 Session 存儲數據
- 過濾與清理
- 上下文編碼
- 驗證Validation
- 表單_Forms
- 讀取配置
- 分頁 Pagination
- 使用緩存提高性能
- 安全
- 加密與解密 Encryption/Decryption
- 訪問控制列表
- 多語言支持
- 類加載器 Class Autoloader
- 日志記錄_Logging
- 注釋解析器 Annotations Parser
- 命令行應用 Command Line Applications
- Images
- 隊列 Queueing
- 數據庫抽象層
- 國際化
- 數據庫遷移
- 調試應用程序
- 單元測試
- 進階技巧與延伸閱讀
- 提高性能:下一步該做什么?
- Dependency Injection Explained
- Understanding How Phalcon Applications Work
- Api
- Abstract class Phalcon\Acl