2013-12-18 17 views
4

我已經從Laravel的docs以及教程和SO中得到了啓動。但是當我嘗試使用他們的API/class reference時,我仍然遇到同樣的問題。Laravel 4.1文檔如何工作?

舉例來說,我已經能夠使用URL類是這樣的:

URL::to('string') 

這我瞭解從教程。但是,如果我查看Illuminate\Support\Facades\URL的文檔,它不會列出to()方法。

反之,如果我看的Illuminate\Filesystem\Filesystem文檔和我嘗試調用get()方法是這樣的:

$file = new Filesystem; 
$file->get('lorem.txt'); 

我收到以下錯誤

Class 'Filesystem' not found 

我的問題:

  • 我怎麼知道w我可以使用哪些類以及可以調用哪些方法?
  • 我在哪裏可以找到這些信息?
  • 或者我只是想知道Laravel是如何工作的?
+0

很大的問題! – Rafael

回答

9

Laravel使用名爲外觀設計模式,它基本上是爲實例化對象的別名,所以你可以用這種方式:

URL::to('string'); 

而不是

$url = new URL; 
$url->to('string'); 

乘坐看看你的app/config/app.php,你會看到指向Facade的URL別名:

'URL' => 'Illuminate\Support\Facades\URL', 

如果你看一下門面,你會看到它的真正的「內部」的名字(「URL」),在IoC容器:

protected static function getFacadeAccessor() { return 'url'; } 

這種「URL」對象由服務提供者實例某個地方,這個人是綁定到IoC容器中Illuminate\Routing\RoutingServiceProvider

/** 
* Register the URL generator service. 
* 
* @return void 
*/ 
protected function registerUrlGenerator() 
{ 
    $this->app['url'] = $this->app->share(function($app) 
    { 
     // The URL generator needs the route collection that exists on the router. 
     // Keep in mind this is an object, so we're passing by references here 
     // and all the registered routes will be available to the generator. 
     $routes = $app['router']->getRoutes(); 

     return new UrlGenerator($routes, $app->rebinding('request', function($app, $request) 
     { 
      $app['url']->setRequest($request); 
     })); 
    }); 
} 

而且那裏,你可以看到,「網址」,其實,UrlGenerator ->(http://laravel.com/api/4.1/Illuminate/Routing/UrlGenerator.html)

這裏的to()方法:

/** 
* Generate a absolute URL to the given path. 
* 
* @param string $path 
* @param mixed $extra 
* @param bool $secure 
* @return string 
*/ 
public function to($path, $extra = array(), $secure = null) 
{ 
    // First we will check if the URL is already a valid URL. If it is we will not 
    // try to generate a new one but will simply return the URL as is, which is 
    // convenient since developers do not always have to check if it's valid. 
    if ($this->isValidUrl($path)) return $path; 

    $scheme = $this->getScheme($secure); 

    $tail = implode('/', (array) $extra); 

    // Once we have the scheme we will compile the "tail" by collapsing the values 
    // into a single string delimited by slashes. This just makes it convenient 
    // for passing the array of parameters to this URL as a list of segments. 
    $root = $this->getRootUrl($scheme); 

    return $this->trimUrl($root, $path, $tail); 
} 

這是在開始有點混亂,但你要記住:

1)查找的別名。

2)找到Facade並獲取真實的內部名稱。

3)找到ServiceProvider找到真實的類。

1

當您調用類似URL::to('string')的東西時,您通過檢查外觀名稱空間來找到您正在使用的Facade。門面類本身只是點的請求,以應該使用的類,在這種情況下url,這個類在方法registerUrlGenerator()結合到Laravel IoC容器中Illuminate\Routing\RoutingServiceProvider

通過研究綁定,您可以看到,當您使用URL外觀時,實際上使用的類別是Illuminate\Routing\UrlGenerator

API文檔在像Laravel這樣的框架中並不實用,其中Facades通過IoC容器提供對類的訪問,從而從調用代碼中抽象實現。這使得很難找到你實際使用的課程在哪裏。我打算在供應商目錄上運行搜索。如果立面返回url。我尋找能找到綁定的$this->app['url'],然後我可以追蹤到實際使用的類。如果搜索失敗,我會在供應商目錄中簡單搜索('url。至於你的第二個問題,如果你想實例化一個名字空間的類,你必須使用這個名字空間。

$filesystem = new Illuminate\Filesystem\Filesystem(); 

你不會真的需要做到這一點,因爲這類使用別名files綁定到IoC容器是通過門面File讓您完全通過靜態調用的類。

或者,如果你想要自己的Filesystem類副本,最好使用IoC容器來獲得它。

$filesystem = App::make('files');