Ad

Class Not Found After Ajax Request - October Cms

- 1 answer

For my application in october cms I'd like to be able to send a mail with the click of a button. I call the php file with an ajax request, however when the button gets clicked I get the error 'class not found' for whichever class I use, doesn't matter which. I already added the file to .htaccess to be able to run it on the button click. I included all classes at the top of the file. Also when I turn it into an artisan command and run it with php artisan now:send:mail it works without any issues. I already tried to run composer dump autoload and composer dump autoload -o. Code is down below, Any idea what I can do to make this work, or in what other way it could be done?

Thanks in advance!

Part of theme.js:

$.ajax({
    url : '/plugins/test/notifications/ondemand/OnDemand.php'
}).done(function(data) {
    console.log(data);
});

OnDemand.php:

<?php

namespace Test\Notifications\OnDemand;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Test\Poles\Components\Metrics;
use October\Rain\Argon\Argon;

class OnDemand
{
    public function send()
    {
        $date = Argon::now(config('app.timezone'))->format('Y-m-d');
        // get some data
        $array = ['date' => $date, 'score' => $score, 'CO2' => $CO2, 'scorecar' => $scorecar, 'scorebike' => $scorebike];
        $email = "[email protected]";
        Mail::sendTo($email, 'daily.mail', $array);
    }
}

$mail = new OnDemand;
$mail->send();
Ad

Answer

I'm not sure whether you want to do this as part of a custom October plugin you've developed or simply inside a regular October template. However, the absolutely simplest way of having an ajax button to send an email would be as follows:

1) Create a new mail template in the October backend in Settings | Mail Templates

2) In the "CMS" section of October, create a new blank Page

3) For the "Markup" section of the new page, the button HTML:

<button data-request="onPressButton">Send</button>

4) For the "Code" section of the new page, the following PHP:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Test\Poles\Components\Metrics;
use October\Rain\Argon\Argon;


function onPressButton()
{
    $date = Argon::now(config('app.timezone'))->format('Y-m-d');
    // get some data
    $array = ['date' => $date, 'score' => $score, 'CO2' => $CO2, 'scorecar' => $scorecar, 'scorebike' => $scorebike];
    $email = "[email protected]";
    Mail::sendTo($email,'daily.mail', $array);
}

That's it. As long as you include JQuery and {% framework extras %} in your October page layout, the above will work.

The principal is the same if you're adding this within a custom plugin that you've developed, but the HTML and PHP would obviously go into their individual files within a component if you did it that way.

Ad
source: stackoverflow.com
Ad