[Solved]Check content of a view and send mail if not ok

Hi everyone,
With cakephp 2.9 I have this need:
I should check if there is a specific tag on the page that appears.
if it is present, must not do anything else must send me an email.

At this point I think to create a helper and recall a method in the default.ctp

with something like:

s = this-> fetch (‘content’);
result_check = this-> myHelper->debug_content ( s); echo s;

In the myHelper the function:

public function debug_content( s) { pos = strpos ( s, "<div class = \" box-body \ ">"); if ( pos === false) {
echo “Error tag is not present!”;
return false;

In the AppController:

public $ helpers = array (…, ‘myHelper’);

and up to here ok … but now?
How do I recall the component Email (personalized by me) to send an email?
And where do I call it?
How would you do?

Thank you,

No one who has any ideas on how to do it?

Well, components can be accessed from controllers, not views, so the approach you’re describing violates the MVC concept. This very much sounds like the sort of thing that middleware would handle, but you’d need to upgrade to at least CakePHP 3.3 to get middleware support, I believe. So, maybe no surprise that you haven’t got any suggestions so far.

When you say “must no do anything else”, what does “anything” include? Should it stop the database from being updated if it was an edit that just happened? Because doing anything about that at the time of the view is far too late.

If you just mean that no output should be sent to the user, one possibility would be to trigger an event, with a handler registered via your AppController. The handler could send the email and then throw an exception. Bit of a hackish way, but it should work, I think…

Thank you for your reply,
I solved the following way:

at the top of myHelper I put:

App::uses(‘CakeEmail’, ‘Network/Email’);

then I rewritten debug_content like this:

public function debug_content($s){
$pos=strpos($s, “<div class=“box-body”>”);
if ($pos === false) {
echo “Layout error!
An e-mail been sent to our development team, soon the problem will be solved , thanks!”;
$Email = new CakeEmail();
‘host’ => ‘host.ext’,
‘emailFormat’ => ‘html’,
‘transport’ => ‘Smtp’

  $Email->subject('CAKEPHP layout DEBUG');
  $Email->send("error in " . $_SERVER["REQUEST_URI"] . " plan solution as soon as possible");


and this works …
I do not know if it violates MVC but it works, otherwise I do not know how to solve it with 2.9, I did not need anything else … write on the db … maybe to make a sort of log … I had not really thought about it … interesting idea … but I will start to think about it if it should serve me … and certainly not with 2.9

Thank you!