CI load library function using CI instatnce - codeigniter

<?php
$CI =& get_instance();
$CI->load->library('MenuList','menu');
print_r($CI->menu->selectMenu('admin'));
?>
I have the above code in my php page. I want to load the function selectMenu() from the Menulist page in the Libraries.
I have below in my Menulist page:
But i get nothing from $CI->menu->selectMenu('admin')
How to correctly load the data from this kind of loading?
UPDATE:
I have this in my autoload:
UPDATE:
I used model as follows:
<?php
$CI =& get_instance();
// $CI->load->library('MenuList','menu');
$CI->load->model('folder/MenuList','menu');
$menu = $CI->menu->selectMenu($UserProfile);
?>
I can get result.
Why didnt it work with load library and worked with load model?

if you are using new library that assign in autoload.php file:
$autoload['libraries'] = array('MenuList');
And where you using this library:
$CI->load->library('MenuList');

It will work:
$CI->load->library('MenuList');
$menu = $CI->menulist->selectMenu($UserProfile);
So something is wrong in the loading. Look in system\Loader.php:
/**
* Library Loader
*
* Loads and instantiates libraries.
* Designed to be called from application controllers.
*
* #param string $library Library name
* #param array $params Optional parameters to pass to the library class constructor
* #param string $object_name An optional object name to assign to
* #return object
*/
public function library($library, $params = NULL, $object_name = NULL){}
You use menu as the second argument - this is an error.
Here's the fix:
$CI->load->library('MenuList', [], 'menu');
$menu = $CI->menu->selectMenu($UserProfile);

Related

How to call model from public folder in Laravel?

I'm trying to call add an item to a model through my public folder in Laravel but I'm getting this error:
Fatal error: Uncaught Error: Call to a member function connection() on null in [..]/Illuminate/Database/Eloquent/Model.php
Here is the file in my public folder:
<?php
require __DIR__.'../../../bootstrap/autoload.php';
use Illuminate\Database\Eloquent\Model;
use \App\Models\Cdr;
$id_ex = '11';
$cdr = new Cdr();
$cdr->id_ex = $id_ex;
$cdr->save();
Do I need to start the app somehow before this? I've also tried calling a method inside a controller from the public folder, but it gives me the same error. For example:
CdrController::storeCdr($id_ex);
My model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Cdr extends Model
{
public $table = 'cdr';
protected $dates = ['deleted_at'];
public $fillable = [];
public $guarded = ['id'];
/**
* The attributes that should be casted to native types.
*
* #var array
*/
protected $casts = [];
/**
* Validation rules
*
* #var array
*/
public static $rules = [];
}
Right now, your script hasn't got a clue of it's environment. It doesn't know there is a database or even a framework for that matter, you have to actually boot the app to make this information available.
You can do this with
$app = require __DIR__ . '../../../bootstrap/app.php';
If this doesn't work, you need to boot at least the ConsoleKernel. Check app/Console/Kernel.php and it's superclass ConsoleKernel for an example how to do that.

How can I override the functionality of laravel's "script" and "style" functions?

I basically have this in various places in my code:
{{ HTML::style('css/someFile.css') }}
As well as:
{{ HTML::script('js/someFile.js') }}
Now, the problem is I might have several of these. I want a way that I can override all of these with one "super" function that will combine all the CSS files into one, and the JS into one. Basically, i want to write my own file combining logic.
How would I go about hooking into the "style" and "script" functions so that I can create a new function that prints out the css and js as one? I know how to write the logic, but I need to know how to override those two functions?
You may use CodeSleeve/asset-pipeline. This package will automatically combine all of your assets (css/js) into a single file and also it'll minify the code into one single file.
This means that, the browser will make only a single request and also the size of the file could be reduced as well.
I assume you want to change the laravel HTML::script & HTML::style method. You can do this by going to:
/app/vendor/laravel/framework/src/Illuminate/Html/HtmlBuilder.php
In this file the HTML builder methods are specified (style, script, image, link etc.)
To modify the script/style methods scroll down till you see:
/**
* Generate a link to a JavaScript file.
*
* #param string $url
* #param array $attributes
* #param bool $secure
* #return string
*/
public function script($url, $attributes = array(), $secure = null)
{
$attributes['src'] = $this->url->asset($url, $secure);
return '<script'.$this->attributes($attributes).'></script>'.PHP_EOL;
}
/**
* Generate a link to a CSS file.
*
* #param string $url
* #param array $attributes
* #param bool $secure
* #return string
*/
public function style($url, $attributes = array(), $secure = null)
{
$defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');
$attributes = $attributes + $defaults;
$attributes['href'] = $this->url->asset($url, $secure);
return '<link'.$this->attributes($attributes).'>'.PHP_EOL;
}
As you can see this is exactly what the methods does. You said you know how to write the logic, so here you can edit it.
Good luck!

How to use com_users model class in my custom component in joomla

I have created a custom component in Joomla 2.5. In this component I want to fetch all the user 's are available in com_users.For this I want you to know, How can i use com_users model class in to my component. Any one have suggestion's to how to do it.
Depending on where you want use the model you can simply ask Joomla! to load it for you.
In a JController class or sub-class you can call getModel passing in the model name and the components prefix...
e.g.
JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_users/models/');
$model = $this->getModel($name = 'User', $prefix = 'UsersModel');
It may be necessary to add the path of the external model you want to load using JModel::addIncludePath() as show above.
Or if you're sure of the model name and the class prefix you could use JModel's getInstance() to create the desired model object... e.g.
$model = JModel::getInstance('User', 'UsersModel');
Alternatively in a view you could:
$myModel = $this->getModel('myOtherModel');
$this->setModel($myModel);
N.B. In the first line we're passing our desired models name, normally you call getModel without any params to load the default model for your components view controller. In the second line, as we're only passing the model to setModel() it won't make it the default model the view uses.
When we want to use our model objects later on we can specify which we want to use like this:
$item = $this->get('Item');
$otherItem = $this->get('Item', 'myOtherModel' );
The first line uses the view's default model (because we have specified one in the optional parameter). The second line uses the getItem() from myOtherModel.
That's all works because JView (in libraries/joomla/application/view.php) has these methods:
/**
* Method to get the model object
*
* #param string $name The name of the model (optional)
*
* #return mixed JModel object
*
* #since 11.1
*/
public function getModel($name = null)
{
if ($name === null)
{
$name = $this->_defaultModel;
}
return $this->_models[strtolower($name)];
}
/**
* Method to add a model to the view. We support a multiple model single
* view system by which models are referenced by classname. A caveat to the
* classname referencing is that any classname prepended by JModel will be
* referenced by the name without JModel, eg. JModelCategory is just
* Category.
*
* #param JModel &$model The model to add to the view.
* #param boolean $default Is this the default model?
*
* #return object The added model.
*
* #since 11.1
*/
public function setModel(&$model, $default = false)
{
$name = strtolower($model->getName());
$this->_models[$name] = &$model;
if ($default)
{
$this->_defaultModel = $name;
}
return $model;
}
Try something like this
if(!class_exists('UsersModelUser')) require(JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_users'.DS.'models'.DS.'user.php');
You can add correct path the model from admin side or front end.
The VM2.x component is using like this way.
Or you need only some details about user you can use.
$user = JFactory::getUser();
Hope this may help you..

virtuemart onafterConfirmorder , execute code after order

Is there any event like onafterConfirmorder in virtuemart ?
like in joomla onAfterRender,onBeforeRender events.
i want to execute code after order has been confirm .
Maybe the path is different in Joomla 2 or 3?
In Joomla 1.5 there is no path like:
ROOT_PATH\folder_name\administrator\components\com_virtuemart\models\order.php
Only the following path exists:
ROOT_PATH\folder_name\administrator\components\com_virtuemart\classes\ps_order.php
Better you have to create a plugin for this concept.
First you need to find the ORDER section in Virtumart. The following model file contains all the order functionality.
ROOT_PATH\folder_name\administrator\components\com_virtuemart\models\order.php
In this file you have to find where the order has been completed. In that section once the order was completed you have to trigger this plugin process your functionality.
You can call any event of plugin which is defined in that plugin.
$dispatcher = JDispatcher::getInstance();
$data = array($argu1, $argu2); // any number of arguments you want
return $dispatcher->trigger(onAfterRender, $data);
Then it will trigger the onAfterRender event in plugin which you created.
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.plugin.plugin' );
/**
* Example system plugin
*/
class plgSystemExample extends JPlugin
{
/**
* Constructor.
*
* #access protected
* #param object $subject The object to observe
* #param array $config An array that holds the plugin configuration
* #since 1.0
*/
public function __construct( &$subject, $config )
{
parent::__construct( $subject, $config );
// Do some extra initialisation in this constructor if required
}
/**
* Do something onAfterRender
*/
function onAfterRender()
{
}
}
Like this you have to create your plugin..
All the best....

CodeIgniter: MVC and Widgets?

I'm new to codeigniter and building web applications using MVC. I'm trying to wrap my head around how I would implement widgets in a modular fashion in my application. My question is more theoretical at this point. I don't have actual code to show.
What I want to know is this, how would I construct a data-driven widget in such a way that I can simply drop it on to any page that I want. For example, let's say I have a widget called Widget. I've created a model file called /models/widget_model.php. I then have a controller file called /controllers/widget.php. Obviously my controller will use the model to grab necessary data from my database. What I don't understand is how to use this as a widget dropped onto multiple views. What I'm seeing and understand so far is how to use a controller to drive a specific view. So it's basically like one controller is used per page. What would be the process of using this widget in a modular fashion I guess?
What you search for is HMVC. There are two common library/packages you can use : Modular CI or HMVC. With that, you can actually put something like <?php echo Modules::run('module/controller/method', $param, $...); ?> as a widget, in your view files.
You can do it via drivers. Send the controller as an object reference to the driver to use view class. Then you just load drivers and use them as plugins.
Edit:
Here is the code I use in my application:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter base widget driver
*
* #author Alex
* #version 1.0.0
*/
class Basedriver {
/**
* Current specified controller.
* #var CI_Controller
*/
public $controller;
/**
* Contents of the driver which should be outputted or returned.
* #var string
*/
protected $contents;
/**
* Loader Class
* #var CI_Loader
*/
protected $load;
/**
* Constructor function for Basedriver class
*/
public function __construct()
{
$this->controller =& get_instance();
$this->load = $this->controller->load;
}
/**
* Renders driver data into specified output. If $echo_contents is true,
* output is echoed to the client, otherwise it is returned.
* #param boolean $echo_contents Specifies whether the content should be outputted or returned as string
* #param mixed $params Array of parameters which should be sent to the driver
* #return string Returned driver data if $echo_contents is set
*/
public function render($params = NULL, $echo_contents = true)
{
$this->parse_params($params);
$this->run();
if ($echo_contents)
echo $this->contents;
else
return $this->contents;
return NULL;
}
/**
* Default run function for all drivers, should be overidden by extending classes.
*/
protected function run()
{
$this->contents = NULL;
}
/**
* Parses parameters and sets them as variables.
* Default variables need to be defined in extending class
*/
protected function parse_params($params)
{
if ($params === NULL) return;
foreach($params as $variable => $value)
{
if (isset($this->$variable))
$this->$variable = $value;
}
}
}
/* End of file Basedriver.php */
/* Location: ./application/libraries/Basedriver.php */
Load class is there to allow you to use view class and controller is there to allow you to use database functions and to give you some other access if you need it. This class needs to be loaded before all other drivers (widgets) and all drivers (widgets) need to extend this class. You can do this by adding 'basedriver' in $config['libraries'] array in application/config/autoload.php.
Example Driver Widget:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Example extends Basedriver
{
protected $parameter1 = 'defaultvalueparam1';
protected $parameter2 = 'defaultvalueparam2';
protected function run()
{
// Widget logic here...
// you can use $this->load->view and $this->controller->db here
$this->contents = 'final_processed_data_here';
}
}
/* End of file Example.php */
/* Location: ./application/libraries/Example/Example.php */
To use the driver which extends Basedriver as a widget, example:
$this->load->driver('example');
$this->example->render(array('parameter1' => '1', 'parameter2' => '2'));
I think you could simply using CI's view system. You create a view per widget, then you inject any variable you want from your model, and finally you display the resulting HTML anywhere you want. I can't think of any particular difficulty.

Resources