Including external code on magento observer - magento

I want to include an external configuration file in a Magento Observer Model
My observer is in magento/app/code/local/Mycompany/Mymodulue/Model/Observer.php and i want to put my code in magento/scripts/examplename.php
I don't want to use a helper because this code will also be used in other scripts paced in that folder and code repetition is not a good practice
Which is the best way to do this?

Magento is a php application and you can use include() and require() everywhere in your code however this is a bad idea to place classes and spaghetti code inclusions in random places on your server as Magento already includes lib folder and autoloader for those if you follow the naming conventions.

The way I found to do what i was looking for is:
require_once BP.DS.'scripts'.DS.'examplename.php';
where BP and DS are Magento constants, BP=Base Path and DS= Directory Separator

Related

External PHP Include in Laravel

We run a main website which is not made in Laravel. A specific script on this website however is made in Laravel. We need this script (or to be exact, a specific view inside of it) to fetch some resources from the main website (PHP files which mainly include HTML). When, inside the Laravel application, we try to include these resources, we can not - as they don't exist in the Laravel project workspace. This results in an error that the file does not exist. Attempting to climb out of the project (../../../file.php) does not seem to help either.
We're including it this way:
<?php
include '~/template/nav.php';
?>
We don't wish to include this file into the actual Laravel project, as that would require having to update it twice to ensure they remain equal. Is there any way for us to include this "external" file in our view? Every bit of research done seems to suggest adding it into the project, which would just cause twice the amount of administration on updates.
Cheers!
Just faced same problem. My Laravel Project is an API that need's to include outside php file. So what I did was specify entire path:
include($_SERVER['DOCUMENT_ROOT'].'/../../my_example_file.php');
$_SERVER['DOCUMENT_ROOT'] will lead you to Laravel's public folder.

Magento PhpStorm "method not found in class"

I just started to use PhpStorm and I am also not very familiar with Magento but just starting out.
Anyhow after getting all setup with git, my dev server and local database I am getting a lot of the errors: "Referenced method is not found in subject class".
This is not for all methods.. but it does seem to be a large portion of them.
What is causing this?
Some example methods: getProductUrl, getName, productAttribute, getRatingSummary, and on and on.
You should look to http://magicento.com/
This plugin will enable more autocompletion for Magento.
But there's some magic methods that not exists in the code, and of course, will no be clickable.
This plugins enables autocompletion in more than only just methods, it will enable it for templates names (in setTemplate), layout.xml block types, controller actions and more....
A must have when coding for Magento with phpStorm.
Basically, i will take a simple exemple, your IDE is not able to follow a model instansiation like this one:
$product = Mage::getModel('catalog/product')->load($productId);
When you do this, your IDE don't know that $product is an instance of Mage_Catalog_Model_Product. It should look into config.xml to resolve the classname. And so, you can't follow product methods.
Magicento enables this behavior.

Magento Module Development

I need to modify Menu.php located at app/code/core/Mage/Adminhtml/Block/Page/Menu.php. My question is can I change the directory path to local (app/code/local/Mage/Adminhtml/Block/Page/Menu.php) without having any issues in the future?
The best practice for overriding a Magento core class is explained in the following article
You need to create your own basic module, and rewrite the core class to point to your module's class. That class may then extend the existing Magento core class, overriding or extending any methods.
However, you could simply copy the file and its path to the local directory, since Magento's autoloader defaults there first when looking for classes. The problem with this is that, when you decide to upgrade Magento, you must fully copy the new file to your path (if it were modified by the upgrade) and reapply your modifications.
The rewrite/extend is a much more maintainable solution, but the latter is quick and easy. The choice is yours!

How to load a library from Module using codeigniter modular extensions

i need to load a library from my module using Modular Extensions
my structure like this
modules/
modules/categories/library
categories_class.php
modules/categories/controllers/
categories.php
I need to load categories library in categories controller .
any one cane help me?
I see two problems..
Problem 1
According to your question, your categories module is not organized properly. The whole purpose of HMVC is compartmentalizing of code e.x; modules. Given your present question how does that structure allow you to copy your modules folder and paste it into another app? Answer: It doesnt..
Follow the example below
It should be the following from the app root:
/application/
config
modules/
categories/
views
controllers/
categories.php
libraries/
categories_class.php
models
libraries
core
controllers
Problem 2
per the user guide: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
You must prefix the module name in front of anything referenced inside the modules folder.
e.g: $this->load->library('module/library');
or in your case: $this->load->library('categories/categories_class');
I have attempted previously to exclude the modules folder name and have never gotten it to work.
Controllers can be loaded as class variables of other controllers
using $this->load->module(’module/controller’); or simply
$this->load->module(’module’); if the controller name matches the
module name.
Any loaded module controller can then be used like a library, ie:
$this->controller->method(), but it has access to its own models and
libraries independently from the caller.
I have another perspective for this error behavior, that make me spend about 3 hours, actually I am always using combination of Uppercase and Lowercase on my custom libraries on Codeigniter.
For wiredesigz of Codeigniter HMVC, remember the libraries loader behavior as same as CI itself, always use lowercase of your library classes name instead of actual class name (Maybe combination of uppercase and lowercase)

Loading all codeigniter controller at the beginning

I've a little problem.
In a controller i will load a function wich is in otherController.
I just use
$pCtrl = new otherController;
for loading the class in that otherController.
The problem is that at that moment the otherController has not already been loaded. So the class is not available.
Can i find a way to tell codeigniter to load all controllers (folder controller) before doing other thing ?
Thanks for help.
This is bad practice. You should be using a CodeIgniter Library to do this instead.
Though, if you're already doing bad things, you could just open the /controllers directory in your config/autoload.php and configure codeigniter to autoload all available controllers. Not sure if this will work well (and efficiently) in practice, so try using libraries.
Its better to create a helper function or a library ..
oh btw , you can use HMVC also .

Resources