Where should I put this code in Laravel?

青春壹個敷衍的年華 提交于 2019-12-06 13:01:19

问题


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:


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


来源:https://stackoverflow.com/questions/23551363/where-should-i-put-this-code-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!