我开发了这个代码,用php查看德黑兰的时间和日期:
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
echo "Date: " . $fmt->format(time()) . "\n";这段代码工作正常,但是,我想在Laravel的控制器中使用它。
路由:
Route::get('/date', [
'as' => 'date', 'uses' => 'HomeController@date'
]);主计长:
public function date() {
$fmt = new IntlDateFormatter("fa_IR@calendar=persian", IntlDateFormatter::FULL,
IntlDateFormatter::FULL, 'Asia/Tehran', IntlDateFormatter::TRADITIONAL);
echo "Date: " . $fmt->format(time()) . "\n";
}结果是:
未找到类‘App\Http\Controller\IntlDateFormatter’
我知道Laravel中没有IntlDateFormatter类,但这里使用的是php类。我犯了什么错?
发布于 2016-02-05 11:01:02
谢谢你的回应,我忘了补充
使用IntlDateFormatter;
因此,未找到的错误通过添加解决了。
发布于 2016-02-05 11:03:00
您需要导入类所在的命名空间。
请记住,app\文件夹是PSR-4加载的,因此,如果您的类是在名为IntlDateFormatter.php的文件中定义的,则需要将该文件放在app\中的某个位置。然后在控制器中导入类:
// in your controller
namespace App\Http\Controllers;
use App\{your class location}\IntlDateFormatter;发布于 2016-02-05 11:08:15
下面是一个在Laravel中使用自定义类的示例:
<?php
namespace YourProject\Services;
class IntlDateFormatter{
public static function dateFormat(){
/*Do your date formatting here and return desired result*/
}
}我建议您将此文件保存在应用程序目录中。在config/app.php别名数组中的下一步,添加
'IntlDateFormatter' => Artisanian\Services\IntlDateFormatter::class,这样您就可以轻松地调用dateFormat函数,如下所示:
\IntlDateFormatter::dateFormat();无论是在你的控制器里还是在你的视野中。
我希望这能帮上忙。
祝好运。
https://stackoverflow.com/questions/35222133
复制相似问题