【正文】
ss logic of your application, your code will be cleaner and easier to maintain.All views are stored within the application/views directory and use the PHP file extension. The View class provides a simple way to retrieve your views and return them to the client. Let39。 Redirecting With Flash Data 視圖合成 嵌套視圖 基礎(chǔ)class Location_Repository { public function save(Location $location, $user_id) { // Store the location for the given user ID... }}現(xiàn)在我們完成了應(yīng)用程序三個功能的分離,數(shù)據(jù)庫操作被隔離起來,我們就可以在services和controllers里使用respositories,而不用擔(dān)心切換數(shù)據(jù)儲存方式給應(yīng)用程序帶來影響了。繼續(xù)舉例,我們需要一個坐標(biāo)庫來儲存坐標(biāo),我們可以用任何方式儲存:?php namespace Trackler\Repositories。use Trackler\Entities\Location。繼續(xù)用Trackler舉例,應(yīng)用中有一個表單,用戶可以輸入自己的GPS坐標(biāo)。 $thislongitude = $longitude。 public $longitude。在例子中我們有一個location,它具有經(jīng)度和緯度兩個屬性:?php namespace Trackler\Entities?,F(xiàn)在我們把功能分為三個部分:entities,services和repositories。讓我們來嘗試下不同的方法。最佳實踐我們經(jīng)常聽到這樣一句話:控制器應(yīng)該保持簡潔!但是在實踐中我們怎么才能做到呢?問題的關(guān)鍵在于我們怎么理解模型這個詞。自動加載Laravel框架的自動加載器讓我們能夠非常容易的使用模型和類庫。內(nèi)容如下:?phpclass Printer { public static function write($text) { echo $text。創(chuàng)建一個庫類和創(chuàng)建普通的類一樣容易,只需要把它放到libraries文件夾中。類庫類庫是框架中能夠?qū)崿F(xiàn)某種功能但是不屬于某個具體應(yīng)用的類的集合。 服務(wù)器交互例如,可能你正在寫一個博客程序,那么你就需要一個post模型。 數(shù)據(jù)庫交互模型中最典型的邏輯是商業(yè)邏輯。 自動加載 模型這個事件會接收需要實例化的類名,然后返回給你一個控制器實例。為控制器實例注冊一個事件:Event::listen(Controller::factory, function($controller){ return new $controller。注: 在使用控制器的依賴注入前,你應(yīng)該閱讀IoC容器文檔。})。controller: user39。這非常簡單,只需要在IoC容器中注冊你的控制器即可。 public function get_index() { // } public function post_index() { // }}在我們建立CRUD方法的時候這種風(fēng)格非常的友好。為控制添加REST風(fēng)格屬性:class Home_Controller extends Base_Controller{ public $restful = true。index39。注: 當(dāng)使用嵌套控制器的時候,控制器的注冊順序總是按目錄的層次從深到淺。39。.39。 路由過濾器嵌套控制器控制器可以存放在application/controllers目錄的任意層次的子目錄中。Larvel框架自帶了這個過濾器,你可以在application/。csrf39。在這個例子中我給POST請求附加了一個csrf過濾器。post39。csrf39。before39。而在這個例子中,我們聲明的是不需要過濾的控制器方法。))。, 39。)except(array(39。, 39。給多數(shù)控制器附加過濾器:$thisfilter(39。在這個例子中auth過濾器會在action_index()和action_list()方法前運(yùn)行,用戶必須登錄才能訪問這些頁面。list39。index39。auth39。before39。login39。過濾器。我們可以在application/39。auth39。)。, 39。給所有請求附加過濾器:$thisfilter(39。行動過濾器行動過濾器可以運(yùn)行在控制方法之前,,同時還可以決定何種HTTP請求會觸發(fā)過濾器?,F(xiàn)在我們就可以在瀏覽器中訪問admin插件包的home控制器了。admin::home39。比如你的插件包名字叫admin,那么插件包控制器可以這樣寫:創(chuàng)建插件控制器:class Admin_Home_Controller extends Base_Controller{ public function action_index() { return Hello Admin!。這部分我們將在[插件包]文檔中作進(jìn)一步了解。.插件控制器插件包是Laravel框架的模塊化管理系統(tǒng)??刂破髀酚傻淖孕畔⑼ǔ6x在application/。這意味著沒有在路由中注冊的控制器方法都是不可見的。注: Base_Controller類繼承于Laravel框架的Controller類。Laravel框架默認(rèn)自帶了一個Home_Controller類。在處理應(yīng)用邏輯方面,控制器和路由沒有什么不同。Laravel框架還允許開發(fā)者在路由中聲明應(yīng)用邏輯,這部分會在路由文檔中詳細(xì)說明。通常情況下,控制器會向模塊請求數(shù)據(jù),然后把數(shù)據(jù)傳遞給視圖,最后返回給用戶。 依賴注入 控制器布局 行動過濾器 控制器路由通過Artisan命令行工具調(diào)用路由:php artisan route:call get api/user/1控制器目錄))。 = 39。, 39。 = 39。, array(39。Registering a named route that points to a controller action:Route::get(39。homeindex39。uses39。log39。after39。wele39。)。, 39。Once a controller is registered, you may access its methods using a simple URI convention:://localhost/controller/method/argumentsThis convention is similar to that employed by CodeIgniter and other popular frameworks, where the first segment is the controller name, the second is the method, and the remaining segments are passed to the method as arguments. If no method segment is present, the index method will be used.This routing convention may not be desirable for every situation, so you may also explicitly route URIs to controller actions using a simple, intuitive syntax.Registering a route that points to a controller action:Route::get(39。admin39。39。)。Registering the home controller with the Router:Route::controller(39。admin39。The Controller::detect method simply returns an array of all of the controllers defined for the application.If you wish to automatically detect the controllers in a bundle, just pass the bundle name to the method. If no bundle is specified, the application folder39。s controllers directory. You can do it in one simple statement. Here39。re unfamiliar with controllers you may want to read about controllers and return to this section.It is important to be aware that all routes in Laravel must be explicitly defined, including routes to controllers. This means that controller methods that have not been exposed through route registration cannot be accessed. It39。})。(:bundle)/panel39。. 不重復(fù),還能讓使用你的代碼的開發(fā)者隨意修改擴(kuò)展包的根(root)URI而不破快擴(kuò)展包中定義的路由!當(dāng)然,你可以在擴(kuò)展包中的所有路由上使用 (:bundle) 占位符,而不僅僅是跟路由(root route)。我們來解析一下這段代碼。Wele to the Admin bundle!39。(:bundle)39。注意到代碼中的 handles 參數(shù)了嗎? 這告訴Laravel加載Admin擴(kuò)展包并處理任何以“admin”開頭的路徑(URI)。admin39。handles39。admin39。 通過此段介紹,你會認(rèn)識到擴(kuò)展包不僅可以通過路由(route)暴露功能,還可以在擴(kuò)展包中注冊路由。可以通過配置擴(kuò)展包,方便的處理請求(request)。})。login39。login39。GET39。}))。login39。39。login39。)){ // 名稱為”home“的路由正在處理當(dāng)前請求(request)!}HTTPS路由(HTTPS Routes)定義路由時,可以通過使用”“參數(shù)指定所生成的URL(或重定向時)采用HTTPS協(xié)議。反查處理當(dāng)前請求(request)的路由是否具有給定的名稱(name):if (Request::route()is(39。)。重定向到命名路由:return Redirect::to_route(39。home39。}))。home39。as39。/39。給路由賦予一個名稱(name)可以方便通過這個名稱(name)動態(tài)生成路徑(URI),即便以后路由變化了,路徑(URI)仍然和你新的路由保持一致。})。dashboard39。, function() { // })。), function(){ Route::get(39。 = 39。Route::group(array(39。注意: 之后(after) 過濾器接收到的參數(shù)是對應(yīng)當(dāng)前請求(request)的 回應(yīng)(Response) 對象。你可以在 application/ 文件中找到這兩個過濾器(filters)。, function(){ // }))。 = 39。, array(39。Defining a filter and URI pattern based filter in one:Route::filter(39。)。, 39。模式過濾器(Pattern Filters)有時你可能需要針對所有包含部分路徑(URI)的請求(request)綁定一個過濾器(filter),例如,你想對以“admin”開頭的路徑(URI)綁定一個叫”auth“的過濾器(filter),以下代碼就是具體實現(xiàn):wD定義一個基于路徑模式(URI pattern)的過濾器(filter):Route::filter(39。role:admin39。before39。panel39。, function(){ //}))。 = 39。, array(39。綁定多個過濾器(filters)到路由:Route::get(39。log39。after39。download39。}))。blocked39。filter39。before39。blocked39。})。home39。filter39。在 application/ 文件中定義了所有過濾器(filters)。如果在路由之前觸發(fā)的過濾器(filters)有返回值,那么這個返回值將被認(rèn)為是對此次請求(request)的回應(yīng)(response)