Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(new MailMessage)->view("email.test")->render() throws "View [] not found" error #28958

Closed
sudkumar opened this issue Jun 26, 2019 · 12 comments
Closed

Comments

@sudkumar
Copy link

  • Lumen Version: 5.8.*
  • illuminate/mail Version: 5.8.*
  • illuminate/notifications Version: 5.8.*
  • PHP Version: 7.2.14

Description:

In my lumen based project, I am using illuminate/notifications and illuminate/mail package to set the notification emails. Before sending notification, I want to preview the rendered content of notification.

I am previewing Mail Notifications by calling the (new MailMessage())->view("emails.testing_template")->render() from a testing route. When accessing the route, I got an error stating View [] not found.

Steps To Reproduce:

  1. Bootstrap an application with lumen
  2. Install the dependencies: composer require illuminate/mail illuminate/notifications
  3. Register the service providers
## bootstrap/app.php
$app->register(Illuminate\Notifications\NotificationServiceServiceProvider::class);
$app->register(Illuminate\Mail\MailServiceProvider::class)
  1. Alias mailer
## bootstrap/app.php
$app->alias('mailer', Illuminate\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);
  1. Create a notification for testing purpose
## app/Notifications/Example.php
<?php
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class Example extends Notification
{
    public function via()
    {
        return ['mail'];
    }

    public function toMail()
    {
        return (new MailMessage())->view("email.test");
    }
}
  1. Create a view for testing
## resources/views/email/test.blade.php
<h1>It works</h1>
  1. Now, create a route for testing the notification
## routes/web.php
$router->get("/test/email", function () {
  return (new App\Notifications\Example())->toMail()->render();
});

Instead of getting,

It works

We will get an error stating

View [] not found

Possible Solution

After digging into the error message, I found that

public function render()
{
return Container::getInstance()
->make(Markdown::class)
->render($this->markdown, $this->data());
}

the render method only respects the $this->markdown, which in my case, is not even set.

There are two possible solutions

  1. Use markdown instead of views.
  (new MailMessage())
-    ->view("email.testing_template")
+    ->markdown("email.testing_template")
      ->render()

This will require no change in the library. But I don't know if it is what is expected by the apis. May be docs should mention it somewhere. As far as I know, Markdown is a super set of Views so any view should just work for markdowns. Please correct me if I am wrong!.

  1. Update the render method to consider the $this->view along with the $this->markdown
  public function render () {
    return Container::getInstance()
      ->make(Markdown::class)
-    ->render($this->markdown, $this->data())
+    ->render($this->markdown ?? $this->view, $this->data())
  }
@driesvints
Copy link
Member

I think it's best that in this case you just use the Laravel framework which was intended to work with these components and not Lumen. Lumen is only intended for highly scalable apis. In any case if you're facing an integration problem with any of the components it's best that you ask on a support channel.

Thanks.

@sudkumar
Copy link
Author

Alright. Thanks @driesvints.

@awebartisan
Copy link
Contributor

awebartisan commented Aug 7, 2019

Hi, I am facing the mentioned issue in Laravel framework's Notification.
Laravel version 5.8.29

I have tried on fresh Laravel's copy and still facing same exception.

Route::get('notification', function(){
    return (new \App\Notifications\Dummy())->toMail('test@test.com');
});

And in toMail() method I am returning like below:

return (new MailMessage)->view('dummy');

Facing below exception:

View [] not found.

@sudkumar
Copy link
Author

sudkumar commented Aug 8, 2019

@awebartisan Yes, I can confirm that. I bootstrapped a new project (5.8.31) and tested. It's still there. It works when markdown is used instead of view

- return (new MailMessage)->view('dummy');
+ return (new MailMessage)->markdown('dummy');

The issue is with the MailMessage->render as mentioned in the first comment (#28958 (comment))

@driesvints Can you please look into it ? I am happy to create a fix PR.

@driesvints
Copy link
Member

Can you upload a repo to github which reproduces the problem?

@sudkumar
Copy link
Author

sudkumar commented Aug 8, 2019

@driesvints
Copy link
Member

@sudkumar there's no "dummy" view in your repo: https://github.com/sudkumar/notification_render_issue_in_laravel/tree/master/resources/views

Please try a support channel. Thanks.

@sudkumar
Copy link
Author

sudkumar commented Aug 9, 2019

I am using the welcome view instead of a dummy view. Please see the Dummy notification:

https://github.com/sudkumar/notification_render_issue_in_laravel/blob/dc8a916e4b3f63d8ec84e79dcd8eb5f4f8fe0524/app/Notifications/Dummy.php#L41-L44

@driesvints
Copy link
Member

@sudkumar can you please try a support channel first? If you can actually confirm the bug feel free to respond back.

@sudkumar
Copy link
Author

Thank you. No problem. :)

@muhtarudinsiregar
Copy link

maybe you can check here, already fixed in #29698

@sudkumar
Copy link
Author

sudkumar commented Sep 5, 2019

@muhtarudinsiregar Thank you for the reference.

I was sure of the bug as it was clear from the code itself and now it is fixed. 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants