I’ve integrated GeoIP into my CakePHP. Now I have to call it from my view-file. I made in my controller such function:
function getCountry($ip)
{
$this->GeoIP->countryName($ip);
}
GeoIP is a included component.
When I wrote in my view globally something like this:
$this->GeoIP->countryName('8.8.8.8') it works well, but, as I remember, this is wrong for MCV architecture. So the right way is to call requestAction for my controller.
Here I have 2 problems: I have to do this in php function which is located in view-file:
// MyView.php:
<?php
function Foo()
{
$this->GeoIP->countryName(...);
}
?>
First mistake is that $this isn’t available inside the function, the second one is how to call getCountry from my component and pass need ip address into $ip?
I’ve tried:
echo $this->requestAction('Monitoring/getCountry/8.8.8.8');
Monitoring is a controller name.
But this returns nothing without any errors. What’s the right way and how to call this in function?
Something like this:
Layout -> View/Layouts/default.ctp (works on any other view/element or block)
Element -> View/Elements/GeoIP.ctp (use an element so you can cache it and don’t request the controller every time)
Controller -> Controller/MonitoringController.php