magento - create a global function - magento

I want to create a function that can be accessed by all *.phtml files. Where should i place this function in the magento framework?

For down and dirty things, you can always define it in index.php. for example, I always put this function there:
function dumpit($obj)
{
print '<pre>';
print_r($obj);
print '</pre>';
}
Then you can quickly call this routine from anywhere, without having to remember all the other overhead function names to get at the helper.

You should create a module and a helper class in that module (Usually MyCompany_Mymodule_Helper_Data by default). Then, add your function to that helper class. You can get to that function in your PHTML like this:
Mage::helper("mymodule")->someFunction();
Hope that helps!
Thanks,
Joe

C:\wamp\www\mydirectory\app\code\core\Mage\Page\Helper\Data.php is my path. I used print_r function as the pr() function.
Put it in Data.php as below.
class Mage_Page_Helper_Data extends Mage_Core_Helper_Abstract
{
function pr($data)
{
echo "<pre>";
print_r($data);
echo "</pre>";
}
}
where page is mymodule.
Call it from any .phtml file with
Mage::helper("page")->pr($abcd);
Hope it helps.

For anyone who's interested I put together a short tutorial on how create a global function in Magento : http://joe-riggs.com/blog/2011/06/create-global-function-in-magento/

Related

How to pass a variable from one controller to another laravel

I'm trying to pass a variable from a controller to some other controller. I'm doing it like this-
SomeController
public function index(){
$var = 'variable';
return redirect()->route('toAnotherController')->with('var', $var);
}
Route.php
Route::get('/anotherController', 'AnotherController#index')->name('toAnotherController');
Another Controller
public function index(){
echo $var;
}
But, this is giving an error "Undefined variable $var".
What is going wrong here?
Is there any other way to do this?
This will help you:
Route::get('/anotherController/{var}', 'AnotherController#index')->name('toAnotherController');
public function index($var){
echo $var;
}
Then use Redirect:
return redirect()->route('toAnotherController',[$var]);
Good Luck ~~~
I hope this is not too late. But I am sure this might help somebody one day!!!
In your SomeController
Add this instance public $attributes;
Add the variable you want to pass to another controller with this
$request->attributes->add(['var' => $request->var]);
You can equally resolve it with the code below on the (Another Controller)controller where you needed it
$var= app('request')->get('var');
N:B - Laravel 5.7. I am not sure of earlier versions

Block not returning anything in module developement Magento

Hi i have just start learning magento module development and for this i am following the pierefay tutorial and i found this very useful. However i am following his tutorial step by step in the fourth step
http://www.pierrefay.com/magento-model-database-tutorial-54#comment-1507
i got stuck at one place
in the block file i have following code and it is suggested it will return entries from database
<?php
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
//on initialize la variable
$retour='';
/* we are doing the query to select all elements of the pfay_test table (thanks to our model test/test and we sort them by id_pfay_test */
$collection = Mage::getModel('test/test')->getCollection()->setOrder('id_pfay_test','asc');
/* then, we check the result of the query and with the function getData() */
foreach($collection as $data)
{
echo $retour .= $data->getData('nom').' '.$data->getData('prenom')
.' '.$data->getData('telephone').'<br />';
}
//i return a success message to the user thanks to the Session.
Mage::getSingleton('core/session')->addSuccess($retour);
print_r($retour);
}
}
but it is not returning anything.
When i copy this code in my indexcontroller or phtml file it shows the list of entries from database.
Can anybody tell me how can i use this code in block file to return data
thanks
ok i got the solution by calling the
<?php
echo $this->methodblock();
?>
in phtml file

how to call method of other module?

I am new in the CodeIgniter framework and PHP. I am trying to call a method which is in another module's controller. For that I am using:
modules::run('addons/demo');
but it does not work. How can I accomplish this task?
include it and it will work but it is a bad practice to use this. Instead create a library and define the method there and it will be accessible in the whole application where ever you load the library. in the application/library create my_library.php
<?php Class My_library{
function common_method(){
echo 'this is a common method';
}
}
And call it in the controller method
<?php Class test_controller extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->library('my_library');
$this->my_library->common_method();
}
}
First just need to load the module
$this->load->module("module_name");
Then call the controller method from the loaded module.
$this->module_name->method_name();

Global Variables in CodeIgniter not Working

I want to generate global variables in CodeIgniter by creating my own library and config file. This is what I wrote ini my library file, let's say globalvars.php. I put it in /application/libraries.
class Globalvars{
function __construct($config = array())
{
foreach ($config as $key => $value) {
$data[$key] = $value;
}
$CI =& get_instance();
$CI->load->library('session');
$CI->load->vars($data);
}
}
I want the user id stored in the session to be available in global variable, so I wrote this in my config file. It's named also globalvars.php. It's in /application/config directory.
$config['user']=$this->session->userdata('id');
I then test to see if it's working by write it in my controller this way.
echo $data['user'];
But I get this error in the browser
Message: Undefined property: CI_Loader::$session
Filename: config/globalvars.php
It seems that the session functions is not defined yet. How can I get it work? What have I missed here? Any help would be appreciated.
You cannot use the session library in config file.
The config files are loaded before any libraries, so $this->session is undefined.
The config.php has to be loaded in order for the Session class to even be initialized, as it reads settings from that file.
A lot of issues with this type of thing (setting some "global" data) can be resolved using a base controller, and extending it in your controllers.
// core/MY_Controller.php
MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct(); // Now the Session class should be loaded
// set config items here
}
}
"Normal" controllers will now extend MY_Controller to take advantage of this.
See: http://codeigniter.com/user_guide/general/core_classes.html for more details.
In addition, when you load->vars(), they are available to the view layer only, it does not create a global variable called $data as you seem to be trying to access. If you do this:
$this->load->vars(array('user' => '1'));
You would access it in a file loaded by $this->load->view() like this:
echo $user; // outputs "1"
See: http://codeigniter.com/user_guide/libraries/loader.html
$this->load->vars($array)
This function takes an associative array as input and generates
variables using the PHP extract function. This function produces the
same result as using the second parameter of the $this->load->view()
function above. The reason you might want to use this function
independently is if you would like to set some global variables in the
constructor of your controller and have them become available in any
view file loaded from any function. You can have multiple calls to
this function. The data get cached and merged into one array for
conversion to variables.
I will say that as an experienced Codeigniter user, the concept of a "global vars" class is a bit wonky and probably unnecessary, especially when it's already so easy to get and set config items. You could definitely run into some confusing issues and variable name conflicts with this method (pre-loading lots of view variables on every request).

Codeigniter reusable sections

I have a table of data from a database that I want to display on various pages of my website. Ideally, i would like to just have an include or something that will go off and get the data, and return the html table. The html and data will be identical everytime I need to use this.
I wanted to know the best way of doing this
Thanks
EDIT
If this helps, something similar to a Django "inclusion" custom tag...for any django developers reading
you need to pass the variable $data to the view method.
This is your code:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
Please change it to this in order to load the $data into the view:
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view',$data);
}
You should use a function in a model to fetch the data you need. Your controller calls the model function and sends the returned information to a view. You don't need to use traditional php includes with Codeigniter. I recommend a review of the user guide. It's very good and will tell you all the basic stuff you need to know to develop with CI. But to get you started, you watn to use Models, Views, and Controllers. Your url will tell CI what controller and function inside that controller to run. If your url is
http://www.example.com/my_controller/load_my_view
Then CI will do what is inside the load_my_view function in the my_controller controller. function load_my_view in turn instantiates a model "my_table" and runs a database query, returns information that the controller sends to the view. A basic example follows:
Your model
class my_table extends CI_Model{
function my_data(){
$this->db->select('column_1,column_2,column_3');
$this->db->from('my_table');
$query = $this->db->get();
if($query->num_rows()>0){
$result = $query->result();
}
else{
$result = false;
}
return $result;
}
}
Your controller
class my_controller extends CI_Controller{
function load_my_view(){
$this->load->model('my_table');
$data['my_results'] = $this->my_table->my_data();
$this->load->view('my_view');
}
}
Your View
<ul id = "my_db_results">
<?php foreach($my_results as $result):?>
<li><?php echo $result->column_1." : ".$result->column_2." ( ".$result->column_3." )";?></li>
<?php endforeach;?>
</ul>
It looks like a good oportunity to use cache: http://codeigniter.com/user_guide/libraries/caching.html
Ok, so here is one thing that worked for me, but its definitely not perfect.
I created a view in which made a call to the model getting the data then put the data into a table. This way, I only have to include this view to put the table anywhere.
I understand this completely ruins the point of having a MVC framework, but hopefully it demonstrates what I want to do...and it works

Resources