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?
Create a service, let's say
Mailer
to use it this way in your controllers or other services:You will need:
Take a look at this article: http://fideloper.com/create-facade-laravel-4
You can create folders for your services, like this:
And namespace them:
And use PSR-4 to autoload your classes via namespace, but adding this to your composer.json:
And executing