2016-01-17 66 views
1

我正在嘗試構建我的應用程序,並且在加載類時遇到了一些麻煩。基本Laravel /編程問題

所以我的第一個問題是:ServiceProviders只是綁定接口,對吧?

我該怎麼辦,我的課程是在應用程序啓動時加載的。

更具體地講,我嘗試包括乒乓球天空的簡碼邏輯: http://sky.pingpong-labs.com/docs/2.0/shortcode

我現在所做的就是讓一個文件夾和簡碼放在那裏:

<?php namespace Modules\Account\Shortcodes; 

use Shortcode; 

class AccountsShortcode 
{ 
    public function register($attr, $content = null, $name = null) 
    { 
     $text = Shortcode::compile($content); 
     return '<div'.HTML::attributes($attr).'>'. $text .'</div>'; 
    } 
} 


Shortcode::register('accounts', 'AccountsShortcode'); 

我也嘗試在PSR-4自動加載中添加文件夾,但它不起作用。

我alreay有一箇中間件:

<?php namespace Modules\Page\Http\Middleware; 

use Closure; 
use Shortcode; 

class PageMiddleware 
{ 
    /** 
    * Run the request filter. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     $response = $next($request); 
     $response->setContent(Shortcode::compile($response->original)); 
     return $response; 
    } 
} 

而這部分作品。

那麼,我需要把Shortcode定義代碼放在哪裏,怎樣才能使它被加載,以及什麼是構建它的好方法?

回答

1

根據你所顯示的命名空間,我假設你也使用Pingpong Modules包。如果是這種情況,經過粗略瀏覽他們的文檔,我相信這是你需要做的:

首先,創建你的短代碼類。根據您所提供的信息,你需要在modules/Account/Shortcodes/AccountsShortcode.php創建這個類:

<?php 

namespace Modules\Account\Shortcodes; 

use Shortcode; 

class AccountsShortcode 
{ 
    public function register($attr, $content = null, $name = null) 
    { 
     $text = Shortcode::compile($content); 
     return '<div'.HTML::attributes($attr).'>'. $text .'</div>'; 
    } 
} 

接下來,用你的模塊的服務提供商的短碼註冊到短代碼的類。最後

public function register() 
{ 
    // you can add "use" statements at the top if you'd like to clean this up 
    \Shortcode::register('accounts', \Modules\Account\Shortcodes\AccountsShortcode::class); 
} 

,運行composer dump-autoload,以確保自動加載器知道你的新目錄和類:在modules/Account/Providers/AccountServiceProvider.php,更新register()方法來註冊你的簡碼。

+0

完全正確,我用戶也模塊包;) 非常感謝。 :: class是我錯過的東西:) – mastercheef85

+0

只是好奇心:所以你還把短代碼放在modules \ Modulename \ Shortcode文件夾中?所以這是構建的好方法? – mastercheef85

+0

@ mastercheef85那麼,我從來沒有使用過這兩個軟件包,所以我已經完成了你已經完成的工作+文檔。我會說,如果你有一個特定於模塊的簡碼,那麼是的,在模塊下創建一個Shortcodes文件夾就沒問題。但是,如果您的簡碼可以被多個模塊使用,那麼您可能只需要創建一個簡碼模塊。 – patricus

0

回答你的問題的一部分:

1 - 服務提供商需要app.php config文件夾下,把你的laravel項目中。

+0

你是指哪一個?我已經添加了供應商的一個。 – mastercheef85