I have a custom library that we will call library1. I'm trying to extended library1 with another custom library called library2.
I could do a require_once and include library1 in library2. The other option would be to have library2 use codeigniters load method and load library1 from inside library2.
Any one have any thoughts on why one way is better than the other?
I stumbled onto this problem recently, and for me using require_once proved to be a simple and efficient solution.
First of all, "oop"? I think you are seriously missing the point of mcv, secondly check this here
"CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page. There may be instances, however, where you’d like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location."
Related
I am learning how to write components in joomla-3.x (currently I'm using 3.6.5) and I'm having difficulties figuring out how to execute my own PHP code on every page. Is that even possible? If so how can it be achieved?
So far, I have been unable to add anything globally to all pages unless I modify joomla's own index.php and I'd rather not edit files outside the scope of my component.
Executing code on every page is probably best done using a plugin or possibly a module.
An advantage of using a module is that you can use menu assignment to select which pages the code should be enabled on.
I wrote a component in Joomla 2.5 and I use this
JSubMenuHelper::addEntry('Alpha', 'index.php?option=com_teams&task=showA');
JSubMenuHelper::addEntry('Beta', 'index.php?option=com_teams&task=showB',true);
JSubMenuHelper::addEntry('Gamma', 'index.php?option=com_teams&task=showC');
...
in each of the view.html.php files to switch from on view to another.
This works fine, but it is annoying if I add or change a menu item, because I have to change it in all the files individually.
What is the best (and conform to MVC design) way to accomplish that? I guess just simply use the php methods (include or require) isn't the way to go.
Instead of calling that in each of the view.html.php files, I usually call that specific code, in controller.php or controllers/*.php, so that it is kind of a "global" code for my components.
I'm not 100% sure if it is the best way to do it in MVC, but as in some comments above, you can also use helper functions to call that code, and avoid maintaining it in multiple points.
Although, I am pretty sure that I have found this instruction (calling in the controller) in some Joomla! tutorial, or in another Joomla! component, so it's generally a good practice so far.
My team of coworkers and me have decided to rewrite a legacy app in Yii but the management have a strict policy that all the functionality must remain as it is, so we have to use old modules until they are ported to Yii, these modules are written in a procedural manner and every one of them has a php file that is included in the general index.php. The general index.php does three things:
Starts the session and adds variables to it.
Creates the db connection.
Renders the main php file of the requested module.
How can we use the old modules once we begin to use Yii?
We have looked at URL Management and the logic would be really simple: If the url matches an old module, render it with renderFile() else let do Yii the work, however we don't know if this is the best approach.
Should we consider anything else before beginning the process?
I want to know if URLManagement + renderFile() is the way to go?
The URL handling can indeed be used, but then I would simply write a custom URL Rule class instead of using a ton of routes as your config will be a mess.
If you want some alternative suggestions:
To begin with, split out the creation of the app and its execution
require_once($yii);
Yii::createWebApplication($config);
// If you want to run the app:
Yii::app()->run();
That way you are in full control whether the app actually runs or not.
As for the DB. If you are using PDO you are in luck. I would just give Yii a db component the regular way and then modify the general.php to use the PDO instance from Yii (Yii::app()->db->pdoInstance). It will always be loaded after so that should work. If you aren't using PDO, just use 2 connections, it's not that bad unless you have a lot of visitors.
Regarding the session there shouldn't be that much initialization so unless you have a custom handler, move it to Yii as well. The functions are the same anyway so there shouldn't be much of a problem.
Then there are 2 ways of doing things as I see it:
1) general.php goes first.
You would have to make a list of modules in the legacy file and determine if the current requested module was migrated or not. Basically put the module names that are still in general.php in an array, see if the url requires one of those and include general.php (and don't do Yii::app()->run()). The rest go through Yii.
2) Yii goes first.
Have yii do it's magic but intercept the 404 http exceptions. This is easily done with a custom error handler function (http://www.yiiframework.com/doc/guide/1.1/en/topics.error). If you get to the error function again: determine if its a valid legacy module and include general.php.
Both ways are pretty messy but at least like this you get the chance to migrate one module whilst keeping the rest in the legacy file.
Depending on Size ,complexity and Person Months for a software is critical to take any decisions. Of course it is very very advisable to have homogeneous nature of application rather than heterogeneous nature. If modules you mentioned above are the one you intend to convert I suggest you must have a RE-DO in Yii because Yii has strong ORM modules are very very easy to make.
I suggest you should go for a RE-Do
Following may be of your interest
How do you convert an old OOP PHP project into the Yii Framework?
Anybody knows reasonable hooks usage? I wrote 2 projects and have no idea what the used for.
Thanks
Hooks in CodeIgniter are used to extend or override the core functionality -- for example:
EXTEND:
If you want to add some basic analytics to your page you might add a pre_system and a post_system hook to log the length of time the request took (or at least, how long CodeIgniter took to process the request) and to record the requested URL, the user, and the time. (The first part of this hook series could be handled better by the Benchmark class, since it is already loaded).
OVERRIDE:
From the documentation:
cache_override
Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.
From CodeIgniter User Guide Version 2.1.4
CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page. There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.
Not sure what you consider "reasonable" though, but the above summarizes it pretty well. They allow you to add additional behavior to core library classes at various points in the execution cycle.
I'm new to Zend Framework MVC. I love a lot of things about working with an MVC environment, but find myself confused about it structurally sometimes.
I have a simple task, I'd like to flag certain users on our site to track their movements. For this I've set up a simple table in the database, and started to code in my _initTracking() function into the bootstrap. I then realized I was approaching this from the wrong direction - I'd like this to be one of the last functions that fires, to avoid mucking up my tracking entries with header redirects, and to ensure all autoloaded classes are present. How do I do this? Is there an "onBeforeRender" type of function? If there is I couldn't find it.
Thanks
I would suggest using a ZF plugin. You could track user's actions in plugin's postDispatch() or dispatchLoopShutdown() method, depending on how granular your tracking needs to be.
Some reading about ZF plugins - http://framework.zend.com/manual/en/zend.controller.plugins.html
Also a really neat article about request lifecycle in Zend Framework - http://www.eschrade.com/page/zend-framework-request-lifecycle-4b9a4288.
Ended up putting this in the layout scripts. There's probably a better way of doing this, but in my case (having all views I wanted the code to run in under 2 layouts) it was the easiest, and accomplished my goal.
I think the best place for this would be in a postDispatch() hook in your controller.
Have a look at http://framework.zend.com/manual/en/zend.controller.action.html, specifically the section on Pre- and Post-Dispatch Hooks.
This would suit placing your tracking code in a base controller - which your action controllers would extend, keeping the tracking code in one location.