Joomla calling a Helper from within view.html.php - joomla

I can't use a helper function
I've a helper file in site->helpers->document_management.php
I've a class in that file
defined('_JEXEC') or die;
/**
* Document_managment helper.
*/
class Document_managmentHelper
{
function testFunction()
{
$textis= "hello";
return $textis;
}
}
In my site->views->document->view.html.php
I attempt to call the function testFunction:
$callingCard = Jview::loadHelper('Document_managment');
//require_once JPATH_COMPONENT.'/helpers/document_managment.php';
$getprofileinfo = Document_managmentHelper::testFunction();
echo getprofileinfo;
echo "test is ". JPATH_COMPONENT.'/helpers/document_managment.php';
I just get
PHP Fatal error: Class 'Document_managmentHelper' not found in /var/www/vhosts/mydomain.com/httpdocs/components/com_document_managment/views/document/view.html.php on line xx
I can't figure it out

I called the helper's method in default.php of my custom component's view by following method below:
$isEnable = CustomFrontendHelper::getTheMethod('is_enable'); // a static function in the helper

Related

Construct function not working in Laravel helper

I'm using Laravel version 6.x. I have created one helper function called GraphqlApi.
My helper class will look like below:
<?php
namespace App\Helpers;
/**
* GraphqlApi class
*/
class GraphqlApi
{
public function __construct() {
echo "test";die;
}
}
But whenever I call any function from helper class construct function is not being called.
Is there any way to make it work?
if you always want the constructor to be called when you are calling other methods.
First, you can not have static method.
Second, call the method this way.
$graphqlApi = new GraphqlApi();
$result = $graphqlApi->getGuzzleRequest();
This way the constructor will be called.

call a model from a command class in Laravel 5, and pass the command class itself

I have a laravel 5.5 artisan command working, so of course I can use methods like $this->info() and $this->arguments() etc.. it looks like this:
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Compasspointmedia\Julietmenu\Model\Menu;
class MenuManagementCommand extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $signature = 'julietmenu:menu
{action=list}';
protected $description = 'Manages menu elements per the Juliet CMS specification';
public function __construct() {
parent::__construct();
//trying to make this command methods available in Menu
$this->menu = new Menu($this);
}
/**
* Execute the console command.
*/
public function handle()
{
// this works just fine
$this->info('Calling ' . $this->argument('action'));
$action = $this->argument('action');
$this->menu->action();
}
}
Of course, I would like do the actual work in the Model, not the command, using the command like a controller. Here's the model class so far:
namespace Compasspointmedia\Julietmenu\Model;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model {
//
public function __construct($command){
$this->command = $command;
// this says the `arguments` method is present:
print_r(get_class_methods($this->command));
// but, it does not work!
// error: "Call to a member function getArguments() on null"
$this->arguments = $this->command->arguments();
}
public function node(){
echo '--- it works! -----';
}
}
To the point, how do I pass the Command object to the Model so that I can use $this->command->arguments() or the other Command features inside the model?
P.S. I'd be very grateful to know if there's a native "Laravel way" to do this better than passing the entire $this to a constructor.

helper is not loading in Codeigniter model

<?php
class MyModel extends CI_Model {
public function loadData()
{
$CI =& get_instance();
$CI->load->helper('data_helper');
print_r($CI->data_helper); //this is printing nothing
$CI->data_helper->loaditems(); // method is not calling
}
}
function loaditems()
{
echo "hello from load of helper";
}
?>
helper filename is data_helper.php
give me you thought about this why it not working and in which case it will work
Put the file data_helper.php in the /application/helpers directory.
In /application/config/autoload.php load the helper using just the word 'data'. (line 92).
$autoload['helper'] = array('data');
Or you can load it before you need it with $this->load->helper('data');
Then you can use loaditems() from anywhere like a normal function.
You don't need the $CI magic at all.
According to the documentation
$this->load->helper('name');
Where name is the file name of the helper, without the .php file extension or the “helper” part.
which means the following code should work
class MyModel extends CI_Model
{
public function loadData()
{
$this->load->helper('data');
loaditems();
}
}
you can read more about it here
Try data_helper() to call helper function.
data_helper();

Call a method from one controller inside another

Is it possible to call a method from one controller inside another controller in Laravel 5 (regardless the http method used to access each method)?
This is how I have done it. Use the use keyword to make the OtherController available. Then you can call a method from that class on instantiation.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\OtherController;
class MyController extends Controller {
public function __construct()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
Also check out the concept of a Command in Laravel. It might give you more flexibility than the method above.
use App\Http\Controllers\TargetsController;
// this controller contains a function to call
class OrganizationController extends Controller {
public function createHolidays() {
// first create the reference of this controller
$b = new TargetsController();
$mob = 9898989898;
$msg = "i am ready to send a msg";
// parameter will be same
$result = $b->mytesting($msg, $mob);
log::info('my testing function call with return value' . $result);
}
}
// this controller calls it
class TargetsController extends Controller {
public function mytesting($msg, $mob) {
log::info('my testing function call');
log::info('my mob:-' . $mob . 'my msg:-' . $msg);
$a = 10;
return $a;
}
}

set_exception_handler in codeigniter - how to use?

I have a code similar to the following which is not working.
class Abc extends CI_Controller {
static function exception_handler(Exception $ex)
{
var_dump($ex);
echo $ex->getMessage();
exit;
}
function __construct()
{
parent::__construct();
set_exception_handler('exception_handler');
}
function Index()
{
throw new Exception('hehe');
}
}
I get
A PHP Error was encountered
Severity: Warning
Message: set_exception_handler() expects the argument
(exception_handler) to be a valid callback
How to use set_exception_handler in codeigniter
Since exception_handler is a function inside a class, it should be:
set_exception_handler(array('self','exception_handler'));
Or
set_exception_handler(array('Abc','exception_handler'));
Set_exception_handler always expects full name class, including the namespace and omits the "use instruction" for use others namespaces.
In your case the fullname is: 'CI_Controller:exception_handler'
Therefore the correct call is: set_exception_handler('CI_Controller:exception_handler');
If you had one namespace e.g. namespace App;
The correct call is: set_exception_handler('App\CI_Controller:exception_handler');
function Index()
{
function ExceptionHandler($e) use($this)
{
$this->load->view('error', $this->sharedData);
}
set_exception_handler('ExceptionHandler');
throw new Exception('hehe');
}
You should use method of the instantiated Object with access level is public.
final class MyException
{
/**
* Constructor
* trigger getErrorTypesFromDefinedConstant method
*/
public function __construct()
{
// Set global exception handler
set_exception_handler(array($this, 'globalException'));
}
public function globalException($e)
{
// Your code to handle exception
}
}

Resources