2015-12-22 8 views
9

मैं पासवर्ड रीसेट को लागू करने के लिए लार्वेल द्वारा रीसेट्स पासवर्ड ट्रैट का उपयोग कर रहा हूं। मैं जो हासिल करना चाहता हूं वह कतार का उपयोग कर ईमेल भेजना है। आगे खुदाई मैं देखा कि sendResetLink() फ़ंक्शन() एक PasswordBroker वर्ग है जो बदले में कॉल समारोह emailResetLink में कार्यान्वित किया जातालार्वेल में कतार का उपयोग करके ईमेल के माध्यम से पासवर्ड रीसेट लिंक कैसे भेजें 5

$response = Password::sendResetLink($request->only('email'), function (Message $message) { 
      $message->subject($this->getEmailSubject()); 
     }); 

: कोड के माध्यम से खुदाई मैं समारोह postEmail() में नीचे लाइन पाया। emailResetLink फ़ंक्शन निम्नलिखित:

return $this->mailer->send($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) { 
      $m->to($user->getEmailForPasswordReset()); 

जो मैं बस mailer->sendmailer->queue को बदल सकते हैं। क्या यह गैर-प्रोजेक्ट फ़ाइल को संशोधित किए बिना इसे करने का कुछ बेहतर तरीका है?

उत्तर

4

यह वह जगह है जहां लार्वा कंटेनर बचाव के लिए आता है। यदि आपको कोर घटक की कार्यक्षमता पसंद नहीं है तो आप आगे बढ़ सकते हैं और इसे काफी दर्द रहित तरीके से ओवरराइड कर सकते हैं।

सबसे पहली बात आप अपने खुद के PasswordBroker बनाने की आवश्यकता होगी:

namespace App\Auth\Passwords; 

use Illuminate\Auth\Passwords\PasswordBroker as IlluminatePasswordBroker; 

class PasswordBroker extends IlluminatePasswordBroker 
{ 

    public function emailResetLink() 
    { 
     $view = $this->emailView; 

     return $this->mailer->queue($view, compact('token', 'user'), function ($m) use ($user, $token, $callback) { 
      $m->to($user->getEmailForPasswordReset()); 
      if (! is_null($callback)) { 
       call_user_func($callback, $m, $user, $token); 
      } 
     }); 
    } 

} 

यदि आपको अपने ऐप में कहीं रखना चाहते जो कुछ भी आप चाहते हैं करने के लिए अपने नाम स्थान बदलें।

चूंकि सेवा प्रदाता सेवा सेवा प्रदाता deferred service provider है, तो इसे बदलने के लिए आपको अपना स्वयं का प्रदाता बनाना होगा। शायद यह करने के लिए सबसे आसान तरीका है निम्नलिखित की तरह कुछ के साथ Illuminate\Auth\Passwords\PasswordResetServiceProvider का विस्तार है:

namespace App\Providers; 

use App\Auth\Passwords\PasswordBroker; 

class PasswordResetServiceProvider extends \Illuminate\Auth\Passwords\PasswordResetServiceProvider 
{ 

    protected function registerPasswordBroker() 
    { 
     $this->app->singleton('auth.password', function ($app) { 
      $tokens = $app['auth.password.tokens']; 

      $users = $app['auth']->driver()->getProvider(); 

      $view = $app['config']['auth.password.email']; 

      return new PasswordBroker(
       $tokens, $users, $app['mailer'], $view 
      ); 
     }); 
    } 

} 

अंत में अपने config/app.php फ़ाइल में Illuminate\Auth\Passwords\PasswordResetServiceProvider::class को हटा दें और अपने 'providers' सरणी App\Providers\PasswordResetServiceProvider::class जोड़ें।

लार्वेल अब स्टॉक फ्रेमवर्क के बजाए पासवर्डबॉकर के आपके कार्यान्वयन का उपयोग करेगा और आपको फ्रेमवर्क कोड को संशोधित करने की चिंता करने की आवश्यकता नहीं है।

+0

धन्यवाद आपके योगदान के लिए @ marcus.ramsden। मैंने आपके दृष्टिकोण का पालन किया है लेकिन मुझे लगता है कि यह क्यों काम नहीं कर रहा है। मुझे कोई त्रुटि नहीं है। ऐसा लगता है कि यह अभी भी अधिलेखित एक के बजाय मूल वर्ग के 'ईमेल रीसेट लिंक्स' को कॉल कर रहा है। –

+0

उदाहरण के लिए मेरी गलती क्षमा करें, आपके सेवा प्रदाता के पंजीकरण भाग में जो '$ this-> ऐप-> सिंगलटन' '' this-> ऐप-> बाइंड' के बजाय होना चाहिए।साथ ही चीजों को पंजीकृत करने वाले कोर सेवा प्रदाता एक [स्थगित प्रदाता] (http://laravel.com/docs/master/providers#deferred-providers) है। मैं एक पल में चीजें अद्यतन कर दूंगा। –

+0

आपके इनपुट के लिए फिर से धन्यवाद। '$ this-> ऐप-> सिंगलटन 'चाल भी नहीं कर रहा है। अभी भी मूल विधि को बुलाओ। –

5

मुझे पता है कि इसका उत्तर दिया गया है, लेकिन मुझे पासवर्ड रीसेट अधिसूचना कतार में एक और तरीका मिला जो मुझे बहुत आसान मिला। मैंने इसे लार्वेल 5.3 पर परीक्षण किया है।

डिफ़ॉल्ट रूप से, पासवर्ड रीसेट अधिसूचना Illuminate\Auth\Notifications\ResetPassword कक्षा द्वारा लागू की जाती है। इस वर्ग को आपके User मॉडल में sendPasswordResetNotification विधि में तत्काल स्थापित किया गया है और Illuminate\Notifications\Notifiable विशेषता का तरीका पारित किया गया है।

तो, पासवर्ड रीसेट अधिसूचना क़तार में आप बस नई ResetPassword अधिसूचना वर्ग artisan make:notification ResetPassword के माध्यम से बना सकते हैं और की जगह यह इस के साथ कोड है कर सकते हैं:

<?php 

namespace App\Notifications; 

use Illuminate\Bus\Queueable; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification; 

class ResetPassword extends ResetPasswordNotification implements ShouldQueue 
{ 
    use Queueable; 
} 

और अब सिर्फ अपने App\User कक्षा में sendPasswordResetNotification विधि ओवरराइड:

<?php 

... 
use App\Notifications\ResetPassword as ResetPasswordNotification; 
...  

/** 
* Send the password reset notification. 
* 
* @param string $token 
* @return void 
*/ 
public function sendPasswordResetNotification($token) 
{ 
    $this->notify(new ResetPasswordNotification($token)); 
} 
+0

मुझे लगता है कि यह सबसे अच्छा और सरल है, यह काम L5.4 – Nisam

+0

में एक आकर्षण की तरह है प्रासंगिक दस्तावेज: https://laravel.com/docs/5.5/notifications#queueing-notifications – Lemmings19

+0

कतार निर्दिष्ट करने के लिए: '$ this- > सूचित करें ((नया ResetPasswordNotification ($ टोकन)) -> onQueue ('queue-name')); ' – Lemmings19

संबंधित मुद्दे