Sunteți pe pagina 1din 14

event listeners

event listeners

php artisan make:model QuoteLog -m

Schema::create('quote_log', function (Blueprint $table) {

+ $table->increments('id');

+ $table->timestamps();

+ $table->string('author');

+ });

//quotelog.php

protected $table = 'quote_log';

app/Providers/EventServiceProvider.php

protected $listen = [

- 'App\Events\SomeEvent' => [

- 'App\Listeners\EventListener',

- ],

'App\Events\QuoteCreated' => [

'App\Listeners\CreateLogEntry',

],

];

php artisan event:generate

app/Events/QuoteCreated.php

public function __construct($author)

{
$this->author = $author->name;

app/Listeners/CreateLogEntry.php

public function handle(QuoteCreated $event)

$author = $event->author;

$log_entry = new QuoteLog();

$log_entry->author = $author;

$log_entry->save();

//quotecontroller

use App\Events\QuoteCreated;

use Illuminate\Support\Facades\Event;

public function postQuote(Request $request)

{ ...

$quote = new Quote();

$quote->quote = $quoteText;

$author->quotes()->save($quote);

Event::fire(new QuoteCreated($author));

return redirect()->route('index')->with([

'success' => 'Quote Saved !'

]);
//views/index

<div class="input-group">

<label for="email">Your email</label>

<input type="text" name="email" id="email" placeholder="Your email"> </input>

</div>

app/Http/Controllers/QuoteController.php

$this->validate($request,[

'author' => 'required|max:60|alpha',

- 'quote' => 'required|max:500'

+ 'quote' => 'required|max:500',

+ 'email' => 'required|email'

]);

authors migration

$table->string('email');

//quotecontroller

public function postQuote(Request $request)

if(!$author){

$author=new Author();

$author->name = $authorText;

+ $author->email = $request['email'];

$author->save();

}
sending email

//eventserviceprovider

'App\Listeners\SendUserNotification'

php artisan event:generate

//listeners/sendusernotification

$author = $event->author_name;

$email = $event->author_email;

Mail::send(

'email.user_notification',

['name'=>$author],

function($message) use($email,$author) {

$message->from('admin@test.com','Admin');

$message->to($email,$author);

$message->subject('Thank you !');

);

//views/email/user_notification.blade.php

<h1>Thank you for creating a quote ! {{ $name }} </h1>

app/Events/QuoteCreated.php

public function __construct($author)

- $this->author = $author->name;
+ $this->author_name = $author->name;

+ $this->author_email = $author->email;

app/Listeners/CreateLogEntry.php

- $author = $event->author;

+ $author = $event->author_name;

send attachments

$message->attach('storage/images/logo.jpg');

send embedded logo

<img src="{{ $message->embed(asset('/storage/images/logo.jpg'))}}"


style="padding:0px; margin:0px" />

add callback link to email

php artisan make:model AuthorLog -m

//authorlog migration

$table->string('author');

/routes

Route::get('/gotemail/{author_name}',[

'uses' => 'QuoteController@getMailCallback',

'as' => 'mail_callback'

]);
resources/views/email/callback.blade.php

@extends('layouts.master')

@section('content')

<h1>Thank you for registering ! {{$author}} </h1>

@endsection

//quotecontroller

public function getMailCallback($author_name)

$author_log = new AuthorLog();

$author_log->author = $author_name;

$author_log->save();

return view('email.callback',['author'=>$author_name]);

//views/email/user_notification.blade.php

<p> Please register here <a href=" {{ route('mail_callback',['author_name'=>$name])


}} ">Link </a></p>

>php artisan make:model Admin -m

//admin migration

$table->string('name');

$table->string('password');
$table->rememberToken();

//admintableseeder

public function run()

$admin = new Admin();

$admin->name = "admin";

$admin->password = bcrypt("password");

$admin->save();

views/admin/login.blade.php

@extends('layouts.master')

@section('content')

<form action="" method="post">

<div class="input-group">

<label for="name">Your name</label>

<input type="text" name="name" id="name" placeholder="Your name"> </input>

</div>

<div class="input-group">

<label for="password">Your password</label>

<input type="password" name="password" id="password" placeholder="Your password">


</input>

</div>

<button type="submit">Login</button>
</form>

@endsection

//admincontroller

public function getLogin()

return view('admin.login');

public function postLogin(Request $request)

//routes

Route::get('/admin/login',[

'uses' => 'AdminController@getLogin',

'as' => 'admin.login'

]);

//views/admin/login

@extends('layouts.master')
@section('content')

@if(count($errors) > 0)

<section class="info-box fail">

<ul>

@foreach($errors->all() as $error)

{{ $error }}

@endforeach

</ul>

</section>

@endif

@if(Session::has('fail'))

<section class="info-box fail">

{{ Session::get('fail') }}

</section>

@endif

<form action=" {{ route('admin.login') }} " method="post">

<div class="input-group">

<label for="name">Your name</label>

<input type="text" name="name" id="name" placeholder="Your name"> </input>

</div>

<div class="input-group">

<label for="password">Your password</label>

<input type="password" name="password" id="password" placeholder="Your password">


</input>

</div>

<button type="submit">Login</button>
<input type="hidden" name="_token" value=" {{ Session::token() }} "/>

</form>

@endsection

//routes

Route::post('/admin/login',[

'uses' => 'AdminController@postLogin',

'as' => 'admin.login'

]);

Route::get('/admin/dashboard',[

'uses' => 'AdminController@getDashboard',

'as' => 'admin.dashboard'

])

admincontroller

public function postLogin(Request $request)

$this->validate($request,[

'name' => 'required',

'password' => 'required'

]);

if(!Auth::attempt(['name'=>$request['name'],'password'=> $request['password']])){

return redirect()->back()->with(['fail'=>'Could not login']);

return redirect()->route('admin.dashboard');

}
//config/auth

'model' => App\Admin::class,

//admincotnroller

public function getDashboard()

$authors = Author::all();

return view('admin.dashboard',['authors'=>$authors]);

//admin model

use Illuminate\Auth\Authenticatable;

class Admin extends Model implements \Illuminate\Contracts\Auth\Authenticatable

use Authenticatable;

<input type="hidden" name="_token" value=" {{-- Session::token() --}} "/>

// routes

Route::get('/admin/logout',[

'uses' => 'AdminController@getLogout',

'as' => 'admin.logout'

]);

//header.blade.php
<a href=" {{ route('admin.login') }} ">Admin</a>

@if(Auth::check())

<a href=" {{ route('admin.logout') }} ">Logout</a>

@endif

//admincontroller

public function getLogout()

Auth::logout();

return redirect()->route('index');

// check quotes.app/admin/dashboard url

//admincontroller

public function getDashboard()

if(!Auth::check()){

return redirect()->back();

$authors = Author::all();

return view('admin.admin',['authors'=>$authors]);

Route::get('/admin/dashboard',[

'uses' => 'AdminController@getDashboard',

'middleware' => 'auth',


'as' => 'admin.dashboard'

]);

//exceptions/handler.php

return redirect()->route('admin.login');

Route::get('/admin/quotes',function(){

return view('admin.quotes');

})->middleware('auth');

Route::group(['middleware'=>'auth'] , function(){

Route::get('/admin/dashboard',[

'uses' => 'AdminController@getDashboard',

/*'middleware' => 'auth',*/

'as' => 'admin.dashboard'

]);

Route::get('/admin/logout',[

'uses' => 'AdminController@getLogout',

'as' => 'admin.logout'

]);

Route::get('/admin/quotes',function(){

return view('admin.quotes');
/*})->middleware('auth');*/

});

});

S-ar putea să vă placă și