Where should I put this code in Laravel?

2019-04-16 06:32发布

I'm building a site using Laravel4. This is my first Laravel project so I'm still learning how everything fits together and where it should all go.

I just added in the Laravel-Mandrill-Request bundle to my site. I'm able to send out emails from a method in my test Controller that looks like this:

public function sendMeSomething(){
    $payload = array(
        'message' => array(
            'subject' => 'Greetings!!',
            'from_email' => 'xxxx@yyyy.com',
            'to' => array(
                array('email'=>'aaaa@bbbb.com'),
                array('email' => 'cccc@bbbb.com')
                ),
            'global_merge_vars' => array( 
                array(
                    'name' => 'time', 
                    'content' => time()
                    ), 
                array(
                    "name" => "SenderName", 
                    "content" => "Chris"
                    )
                ),
            'merge_vars' => array(
                array(
                    'rcpt' => 'aaaa@bbbb.com',
                    'vars' => array(
                        array(
                            'name' => 'firstName',
                            'content' => 'Christopher'
                            )
                        )
                    ),
                array(
                    'rcpt' => 'cccc@bbbb.com',
                    'vars' => array(
                        array(
                            'name' => 'firstName',
                            'content' => 'Chris!'
                            )
                        )
                    )
                )
        ),
        'template_name' => "sk-hello",
        'template_content' => array(
            array(
                'greetings' => 'why hello!'
                )
            )
    );
    $result = Mandrill::request('messages/send-template', $payload);
    return "check your email. result: " . var_dump($result);
}

Everything works great in the test, so now I'm ready to start building it into my actual site tools.

I would like to abstract it a bit by building a method to dynamically build the payload variable.

My question is, where would be the proper place to put this code? It doesn't seem like it should be in it's own controller because it's a tool that will be called from various places in other controllers. It's not something that interacts solely with a table in my database so it doesn't seem like it should be a model. Should I create a facade for it? Where should this class go?

1条回答
祖国的老花朵
2楼-- · 2019-04-16 07:25

Create a service, let's say Mailer to use it this way in your controllers or other services:

Mailer::send('emails.greetings', 'Welcome!', $user->email);

You will need:

  • Your base service class (Mailer.php)
  • A Service Provider (MailerServiceProvider.php)
  • A Facade (MailerFacade.php)

Take a look at this article: http://fideloper.com/create-facade-laravel-4

You can create folders for your services, like this:

app
│   └── App
│       └── Services
│            └── Mailer
│                ├── Mailer.php
│                ├── MailerServiceProvider.php
│                └── MailerFacade.php

And namespace them:

<?php namespace App\Services;

And use PSR-4 to autoload your classes via namespace, but adding this to your composer.json:

"autoload": {
    "psr-4": {
        "App\\": "app/App",
    },
},

And executing

composer dumpautoload
查看更多
登录 后发表回答