2016-02-05 84 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(); 

无论是在您的控制器或在您的视图。

我希望这个帮助。

祝你好运。