best place to put library in magento - magento

I'm a magento developer.
I am trying to make library file which contains common function like login(id, pass), deleteDir, uploadFile() etc...
So each Controller can use this functions.
But the only file that does similar things is Helper/Data.php.
But Data.php looks like common data container file.
So which file can i put common functions?

You can create helper file (like Helper/Mylib.php) in your module and call the function like
Mage::helper('mymodule/mylib')->myFunction($pId);

You can make the block to put common functions.

Related

Dynamically get the public path for a workbench package in laravel

I'd like to get the path to a package public directory (css etc) based on the package alias.
Is there anything already built into the laravel framework?
In other words something like:
public_path('myalias');
When I'm talking about alias, you would typically "alias" a module by adding the following within your service provider's boot method:
$this->package('namespace/package','alias_name');
For those wondering why someone might want to do this:
We are running a multi domain/subdomain application that makes use of a central piece of code for all of the domains and then specific packages per domain (I'll refer to them as funnels).
Each funnel has its own controllers that can possibly extend base controllers to implement their own functionality while re-using code where they can. They also have their own views.
The funnel refers to its own views by way of something like:
View::make('funnel::path.to.view')
The way we accomplish this is by doing some business logic on page load to only load the FunnelServiceProvider related to that particular domain and aliasing it to "funnel". This way our base controllers can also refer to funnel and not be tied to a particular packages views,includes,blocks etc.
My hope is to do something similar on the views so that I can simply call something like get_funnel_path() to get the path to the funnel that is currently being loaded.
The value could then be used to load css,js,images etc without worrying about the funnel path.
This would allow us to simply copy and paste views from one domain to the next without having to modify all of the paths in potentially multiple files. We could also make use of globally included files in all/most of the views.
An example of this might be the head. The head section should be the same for 99% of the files, however the path where it loads its resources should change based on the funnel.
We use the same naming conventions for css files as well as use sass, imports, merging for all of the funnels; so only the path needs to change.
You can do something like this although it will only work with your own packages and require a bit of work. Because the alias is not really stored somewhere you can easily access you have to do that yourself.
First create some kind of class to store your package names in. I called mine PackageManager:
class PackageManager {
private $packages = array();
public function addPackage($fullName, $alias){
$this->packages[$alias] = $fullName;
return $this;
}
public function getPublicPath($alias){
if(!isset($this->packages[$alias])) return public_path();
$path = 'packages/' . $this->packages[$alias];
return public_path($path);
}
}
Now let's register that class as a singleton in a service provider:
$this->app->singleton('packagemanager', function(){
return new PackageManager();
});
Then, in every package you want to register, add this call in the boot method right next to $this->package():
$this->app['packagemanager']->addPackage('vendor/package', 'alias');
After that you can do this anywhere in your application:
app('packagemanager')->getPublicPath('alias');
If you want a shorter syntax, add this helper function somewhere:
function public_package_path($alias){
return app('packagemanager')->getPublicPath($alias);
}
And just do:
public_package_path('alias');

Overwrite CodeIgniter Common.php

There is a method in code igniter under system/core/Common.php called load_class().
I would like to overwrite this method. Usually to overwrite a code igniter class I create a file such as MY_Common.php however in this case Common.php is a collection of methods and there are no classes that encapsulates them.
So how exactly do I do this?
There's no officially supported way to do this by the built in extending mechanisms. Consider some other way to achieve your goal.
However the functions inside Common.php are all wrapped inside an if checking if the function is already exists or not so you can do the following:
Create your MY_Common.php put somewhere in your project (maybe application/core/ to mirror other similar extends)
Open your index.php file in the root of the project
insert include APPPATH.'core/MY_Common.php'; before the closing require_once BASEPATH.'core/CodeIgniter.php'; line
Now if you have you have a load_class function in your MY_Common.php it will shadow the original version.
The correct/official way to do that is overwriting the core common function into ie. common_helper.php application/helpers and setting up in config/autoload.php

Which is better in codeigniter? Adding a function in a helper or adding a function in an extended base class

In a codeigniter project i have to do some set of stuff in one than one controller.
I code all that stuff in a function and now i need to call whenever necessary.
i think Writing this function in more than one controller is not good.
i have 2 options,
create a helper and write these function in that and include the helper in necessary controllers.
Since i have extended CI base controller (My_Controller) and most of my controllers are extended that controller, i can write this function to my base controller also.
I have confused which one is better, right way?
Which one will speed up the process?
Is the second way slows the site?
They are identical for all intents and purposes.
Using a helper allows you to make the code portable, so you can use it in other projects, or to be called from anywhere in the code base, in the case of a formatting function for example
If you were planning to put it in a controller, then MY_Controller is best bet
Just to help you on you endeavor what i do is: (this is just me)
If i need to use something in the views, i use a helper a custom one or built in.
If i want to do something on a controller that other controller will be using too and don't want it to mess up or crowd my controller i use a library (pretty much you can use a helper but i chose to use a library)
If i want to load lets say a method, to affect Globally or some of the controller i use the base controller. (you could also use helper or library)
The key is you are not restricted to one, choose the best that suits you, as the saying goes, there are many ways to skin a cat, but please don't skin a cat..

codeigniter: accessing a callback from config/form_validation.php config file

Callback functions are not executing from my form_validation config file when called upon. Am I correct when assuming the reason for the non-execution is due to the location of the callback function? For the callback to execute, it will have to be placed inside the config file or have the $config array placed into the controller file? If that is the case, what avenue do you suggest I take or do something completely different to accomplish this?
The more I learn about CI the more I want to use its clean structure.
-Thank You,
Rich
Callback functions are placed within the same controllers or models from which they are called in form validation. Maybe in this case you need to put your callback function into a global controller liker MY_controller.php.

How to serve a MVC-view with image paths from directory?

I'm kind of new to the whole MVC concept, since I just recently started developing a web site using CakePHP framework. Therefore I turn to you asking for how to achieve the following in a best-practice-way.
I want to be able to place a number of pictures in a directory, which then is scanned for all filenames in it. These filenames should afterwards be passed to an arbitrary view, which then loops through all filenames making img tags of them.
It would be nice if this could be done in a general way, so that I could reuse the code for same task but with a different directory name.
I have already tried the following two approaches. Nevertheless, non of these felt like the best way to do it, for some reason.
Create a model with $useTable=false.
Create an ordinary class and import it as a vendor.
What is the best way to achieve what I describe above?
I'm thinking your original idea is the better one, use the Model to traverse/read/write the directory. Think of the File structure as your data source. You can pass the dirname to the model with $this->data and then let it use the File class to retrieve what you need. This would make it portable across Controllers (with loadModel())
Later on down the road if you move your image paths into a DB you only have to re-write the model to take that into account.
I've done the following before: create an e.g ImageManager class and pass it as an IImageManager to the constructor of your e.g ImageController instead of passing a repository. The ImageManager could then do all the work that you mentioned.
EDIT: I did that in .net mvc and don't have experience of CakePHP, but I guess there's a place where you can register the concrete implementation of IImageManager as ImageManager so the framework would know what to pass to the controller's constructor
I would create a helper for this purpose.
Edit: And for trawling through the filesystem use the Folder class from within your helper.

Resources