2016-02-05 86 views
0

我開發這個代碼,查看德黑蘭的時間和日期的PHP:級「應用程序 HTTP 控制器 IntlDateFormatter」未找到

$fmt = new IntlDateFormatter("[email protected]=persian", IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL); 

    echo "Date: " . $fmt->format(time()) . "\n"; 

此代碼工作正常,但是,我想在一個函數中使用它我在Laravel的控制器。

路線:

Route::get('/date', [ 
'as' => 'date', 'uses' => '[email protected]' 
]); 

控制器:

public function date() { 
    $fmt = new IntlDateFormatter("[email protected]=persian", IntlDateFormatter::FULL, 
    IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL); 

    echo "Date: " . $fmt->format(time()) . "\n"; 
} 

,其結果是:

類 '應用\ HTTP \控制器\ IntlDateFormatter' 未找到

我知道Laravel中沒有IntlDateFormatter類,但我除了在這裏使用php類外。我的錯誤是什麼?

+0

添加更多的代碼和說明請 –

+0

嘗試使用'\ IntlDateFormatter' –

回答

0

感謝響應,我forgotted添加

使用IntlDateFormatter;

所以找不到的錯誤通過添加解決。

0

您需要導入類位於命名空間。

記住app\文件夾是PSR-4加載,所以如果例如你的類是在一個名爲IntlDateFormatter.php文件中定義的,你需要把這個文件在你的app\的某個地方。然後在您的控制器導入類:

// in your controller 
namespace App\Http\Controllers; 

use App\{your class location}\IntlDateFormatter; 
+0

這是一個標準的PHP類,我沒有使用應用程序\ {class name},它工作正常,謝謝你的回答。 –

0

這裏是Laravel使用自定義類的一個實例:

<?php 
namespace YourProject\Services; 
class IntlDateFormatter{ 
    public static function dateFormat(){ 
    /*Do your date formatting here and return desired result*/ 
    } 
} 

我asume保存應用程序目錄中文件。 接下來在配置/ app.php內別名陣列,增加

'IntlDateFormatter'  => Artisanian\Services\IntlDateFormatter::class, 

這樣你就可以輕鬆地調用dateFormat功能如下:

\IntlDateFormatter::dateFormat(); 

無論是在您的控制器或在您的視圖。

我希望這個幫助。

祝你好運。