Laravel 错误“找不到类 'App\Http\Controllers\DateTime'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52074184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:00:46  来源:igfitidea点击:

Laravel Error "Class 'App\Http\Controllers\DateTime' not found"

phplaravelpostlaravel-5.6

提问by myckhel

public function recover(Request $request){
    $email = $request->input('email');
    // Create tokens
    $selector = bin2hex(random_bytes(8));
    $token = random_bytes(32);

    $url = sprintf('%s',  route('recover.reset',['selector'=>$selector, 'validator'=>bin2hex($token)]));

    // Token expiration
    $expires = new DateTime('NOW');
    $expires->add(new DateInterval('PT01H')); // 1 hour

    // Delete any existing tokens for this user
    DB::delete('delete * from password_reset where email =?', $email);

    // Insert reset token into database
    $insert = $this->db->insert('password_reset', 
        array(
            'email'     =>  $email,
            'selector'  =>  $selector, 
            'token'     =>  hash('sha256', $token),
            'expires'   =>  $expires->format('U'),
        )
    );

Am trying to implement forgot password when the email form is submitted to forgotPasswordController it generates the below error

当电子邮件表单提交给 forgotPasswordController 时,我试图实现忘记密码它会生成以下错误

"Class 'App\Http\Controllers\DateTime' not found"

“未找到类 'App\Http\Controllers\DateTime'”

This is the picture of the controller the above code is not there is i cant modify it RecoverPasswordController Image

这是控制器的图片上面的代码是不是我不能修改它 RecoverPasswordController Image

At the header i have tried using

在标题中,我尝试使用

use DateTime;

Or

或者

use App\Http\Controllers\DateTime

But still generates same error please help fixing it. thanks in advance

但仍然产生相同的错误,请帮助修复它。提前致谢

回答by delboy1978uk

Above your class definition, import the class with a use statement.

在类定义上方,使用 use 语句导入类。

use DateTime;

The alternative to that is to use the fully qualified namespace in your code. With PHP classes in the global namespace, all this means is a single backslash before the class name:

另一种方法是在代码中使用完全限定的命名空间。对于全局命名空间中的 PHP 类,所有这一切都意味着类名前有一个反斜杠:

$expires = new \DateTime('NOW');

I prefer the first approach, as it allows you to see every class used in that file at a glance.

我更喜欢第一种方法,因为它使您可以一目了然地看到该文件中使用的每个类。

回答by Rafael

DateTime is a PHP Object, so you can declare it using the slash before:

DateTime 是一个 PHP 对象,因此您可以在前面使用斜杠声明它:

new \DateTime();

Or declaring it before you use and instantiating later:

或者在使用之前声明并稍后实例化:

use DateTime;

class Etc
{
    public function test()
    {
        $datetime = new DateTime();
    }
}

回答by Bilal Ahmed

Add a backslash \(to define root namespace)

添加反斜杠\(定义root namespace

$dateTime = new \DateTime();

also you can use classes

你也可以使用类

use DateTime;
use DatePeriod;
use DateInterval;