3

I am writing code for a site that was done in Laravel, I know that it is not recommended to write PHP code within the Blade template, but in this instance I have limited time available.

@php
   if($_POST["submit"]){
        $name = $_POST["name"];
        $email = $_POST["email"];

        $missingName = "<p><strong>Please eneter your name.</strong></p>";
        $invalidEmail = "<p><strong>Invalid Email.</strong></p>";


        if($name){
            $name = filter_var($name, FILTER_SANITIZE_STRING);
        }else{

            $errors .= $missingName;
        }


          $email = filter_var($email, FILTER_SANITIZE_EMAIL);
          $email = filter_var($email, FILTER_VALIDATE_EMAIL);
         if(filter_var($email, FILTER_VALIDATE_EMAIL)){

          }else{
              $errors .= $invalidEmail;
          }
        if($errors){
            $resultMessage = '<div  class="alert alert-danger">' . $errors .'</div>';
        }else{

            $to = "leads@relevant.systems";
            $subject = "DijiJock update request form.";
            $message = "<html>
                         <body>
                         <h2 style='color:black'>DijiJock update request form.</h2>
                <p style='color:green'>Name: $name</p>
                <p style='color:green'>Email: $email</p>
                <p style='color:black'>$name has requested DijiJock updates, please forward all updates to $email.</p>
                         </body>
                       </html>";
            $headers = "Content-type: text/html";


            if(mail($to, $subject, $message, $headers)){
                $resultMessage = '<div  class="alert alert-success">Thank you for the meesage!</div>';

            }else{
                $resultMessage = '<div  class="alert alert-warning">Email not sent! Please try again later.</div>';
            }
        }
        echo $resultMessage;
    }             
@endphp

The PHP code in the middle does not work?

halfer
  • 19,824
  • 17
  • 99
  • 186
Russell
  • 47
  • 1
  • 6

2 Answers2

3

(Here better implement the action in the controller, you can make a helper class, bind it through a service provider, and use it)

In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
    //
@endphp

which is exactly the same as:

<?php

?>

If you need to share global variables best way to do it is to use the method share() of the View facade, and do that on a service provider. https://laravel.com/docs/5.7/views#passing-data-to-views

ex of use case: nested structure for layouts => save global variables with short names, that point to bigger name of tree traversal (+ easy to maintain when we alter the structure, do it once). ex:

namespace App\Providers;

use Illuminate\Support\Facades\View;

class ViewGlobaleVarServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    { // \/\/\/\/\/\/\/\/  <====== !
        View::share('sysAdmDashPgContainerHeader', 'system.admin.dashboard.partials.pageContainer.partials._header');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Here i created a new ServiceProvider, you can use too existing one, like the AppServiceProvider.

If the action to execute is depending on the controller calling, better implement things on controllers, if it's view specific (internal logic). Then @php @endphp is a nice way to achieve that.

Also for sharing variable Know that we can simply use @php @phpend in a layout view (it will be available on all included partials and object that extend it). So you know the nature of @include and @yield,@extend on that matter. However that's not the best practice or recommended. What's recommended is to use the helper View::share() above.

The other way to write code is to create your own directive:

(note all you need is to create one function on a service provider, and you have your own directive, )

And What about sharing function globally? Create and Use a global helper. By creating a file that will contain all the helpers (functions). Then either use the composer** auto loading**. Or bind an include_once through a service provider.

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
1

Use the Controller and blade view method.

Mail::send('emails.YOUR_BLADE_FILE', ['user' => $user, 'request' => $request], function ($message) use ($user, $request) {
     $message->to('EMAIL_ACCOUNT', 'EMAIL_USER_NAME');         
     $message->subject('EMAIL SUBJECT');
});

Try to use this function . This is easy and very clean.

Shanka SMS
  • 644
  • 6
  • 15