如何在laravel中的__construct内部获取变量的值

时间:2019-04-11 10:49:17

标签: php laravel laravel-5

我正在尝试向注册用户发送电子邮件,但是当我尝试运行我的代码时,在“网络”标签中出现了此错误

"message": "htmlspecialchars() expects parameter 1 to be string, object given (View: C:\\xampp\\htdocs\\laravel Projects\\bank\\iscbank\\resources\\views\\emails\\welcome.blade.php)",
    "exception": "ErrorException",

将我的代码放在welcome.blade.php中

<html lang="en-US">
<head>
    <meta charset="text/html">
</head>
<body>
    Name: {{$name}}
    Password:
    {{$password}}
</body>
</html>

这是我的RegisterController.php代码

    <?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Auth\Events\Registered;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;

class RegisterController extends Controller{
    protected function genPass(){
        $limit= 8;
        $chars= 'abcdefghijklmnopqrstuvxwyz1234567890!@#$%^&*()_+=-[]{}\|ABCDEFGHIJKLMNOPQRSTUVXWYZ';
        $chararray= str_split($chars);
        $gen=array();
        for($i=0;$i<$limit;$i++){
            $index=rand(0,strlen($chars)-1);
            $gen[$i]= $chararray[$index];
        }
        return implode($gen);
    }
    protected function genAutKey(){
        return bin2hex(random_bytes(8));
    }
    protected function testFunction() {

     }

    use RegistersUsers;

    protected $redirectTo = '/home';
    private $genPass;
    private $genAutKey;

    public function __construct(){  
        $this->genPass = $this->genPass();
        $this->genAutKey = $this->genAutKey();      
        $this->middleware('admin');
    }

    protected function validator(array $data){
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'phoneNumber' => 'required|numeric|unique:users',
        ]);
    }

    protected function create(array $data){        
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],                
            'Authkey' => $this->genAutKey, 
            'password' => bcrypt($this->genPass),
            'phoneNumber' => $data['phoneNumber'],
            'address' => $data['address'],
        ]);
    }

     public function register(Request $request)  {       
        $validation = $this->validator($request->all());
        if ($validation->fails())  {  
            return response()->json(['errors'=>$validation->errors()->toArray()]);
        }
        else{            
            $this->create($request->all()); 

            // Mail::to($request->input('email'))->send(new WelcomeMail($sentt));
            $mail = Mail::send(['html' => 'emails.welcome'], array(
                'name' => $request->input('name'), 
                'email' => $request->input('email'), 
                'Authkey' => $this->genAutKey, 
                'password' => $this->genPass,
                ), function($message){
                    $message->from(Auth::user()->email, Auth::user()->name);    
                    $message->to(Input::get('email'))->subject(Input::get('subject'));    
            });
            return response()->json(['success'=>'Customer registerd successfully']);
        }
    }
}

请问我在做什么错,我尝试了很多,甚至在网上搜索,但无法找到任何对我有帮助的有用的信息

1 个答案:

答案 0 :(得分:2)

在您的构造方法上,我认为应该$this->genPass而不是$this->$genPass

$this->$genAutKey相同,应该像$this->genAutKey