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?
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."
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.
I have seen hooks in Kohana PHP framework, and they work as some sort of a callback function triggered by a certain event (Kohana's events that is, some sort of method overloading).
I have seen hooks in Wordpress, and I don't know what they are or how to use them (just saw them yesterday).
I'm looking for events in "non-frameworked" php, I cannot find ones.
Do hooks work only in an "event-based" environment? What are they anyway (in general, not just in PHP)? What are they good for if not in an "event-based" environment.
Hooks are, indeed, hooks into an event stack of sorts; a list of values some controller iterates over, and if you have anything registered to that event, the controller can run your custom code. But PHP itself doesn't have anything (useful) like that, so you make it yourself or use the ones you find in your favorite application / system. It's a fairly common way to create a plugin architecture, but can also be used for application control and other things. I've written earlier about my quest for a more universal event and operating set of stack events, including this post here on StackOverflow.
As others have mentioned, PHP is stateless, so where I use them I use them as a simple execution list, and hook every part of my application into it. This way I'm very extensible, and have a basis for a plugin stack as well. (And I'll release it one magical day when I'm bored or retired or just got too much time on my hands, etc.)
You'll find similar stacks and hooks in, for example, WordPress, so a plugin that deals with, say, CSS, will hook itself to the CSS_DEFINITION_EVENT (basically, that part of the WordPress application that writes CSS stuff into the HTML section). This stuff is everywhere. In PHP it only applies (well, mostly) to the limits of the request you get per PHP page (unless you're doing PHP outside the webserver), but all major operating systems, applications, frameworks and systems have some form of event stack. PHP just doesn't have one (seriously) built in.
PHP is stateless, therefore it cannot really have events. They are emulated with manually adding and storing event listeners ( functions to be called ) and then calling said listeners explicitly when something happens in the code. Like a new picture was uploaded or a 404 error occurred.
Are you looking for these?
register_shutdown_function
set_error_handler
set_exception_handler
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Could someone provide a architectural overview of the Drupal 7 control flow? Perhaps in the sense of a flowchart about how a page gets generated. What additional resources would you suggest consulting with regards to how Drupal works?
Drupal can be confusing on this front, partially because it has a relatively deep function stack. Although it's procedural PHP it's purely event/listener driven in its architecture, and there's no simple "flow" in the main PHP script for you to look though. I recently did a presentation on this very subject, and the slides are posted on slideshare, but a quick high-level summary may be useful.
Drupal's index.php file functions as a frontside controller. All page are piped through it, and the "actual" url/path the user requested is passed to index.php as a parameter.
Drupal's path router system (MenuAPI) is used to match the requested path to a given plugin module. That plugin module is responsible for building the "primary content" of the page.
Once the primary page content is built, index.php calls theme('page', $content), which hands off the content to Drupal's theming/skinning system. There, it's wrapped in sidebars/headers/widgets/etc..
The rendered page is then handed back to apache and it gets sent back to the user's browser.
During that entire process, Drupal and third-party plugin modules are firing off events, and listening for them to respond. Drupal calls this the 'hook' system, and it's implemented using function naming conventions. The 'blog' module, for example, can intercept 'user' related by implementing a function named blog_user(). In Drupal parlance, that's called hook_user().
It's a bit clunky, but due to a PHP quirk (it keeps an internal hashtable of all loaded functions), it allows Drupal to quickly check for listeners just by iterating over a list of installed plugins. For each plugin it can call function_exists() on the appropriately named pattern, and call the function if it exists. ("I'm firing the 'login' event. Does 'mymodule_login' function exist? I'll call it. Does 'yourmodule_login' exist? No? How about 'nextmodule_login'?" etc.) Again, a touch clunky but it works pretty well.
Everything that happens in Drupal happens because of one of those events being fired. The MenuAPI only knows about what urls/paths are handled by different plugin modules because it fires the 'menu' event (hook_menu) and gathers up all the metadata plugin modules respond with. ("I'll take care of the url 'news/recent', and here's the function to call when that page needs to be built...") Content only gets saved because Drupal's FormAPI is responsible for building a page, and fires the 'a form was submitted' event for a module to respond to. Hourly maintenance happens because hook_cron() is triggered, and any module with mymodulename_cron() as a function name will have its function called.
Everything else is ultimately just details -- important details, but variations on that theme. index.php is the controller, the menu system determins what the "current page" is, and lots of events get fired in the process of building that page. Plugin modules can hook into those events and change the workflow/supply additional information/etc. That's also part of the reason so many Drupal resources focus on making modules. Without modules, Drupal doesn't actually DO anything other than say, 'Someone asked for a page! Does it exist? No? OK, I'll serve up a 404.'
To understand how Drupal works, you need to understand Drupal's page serving mechanism.
In short, all the calls/urls/requests are served by index.php which loads Drupal by including various include files/modules and then calling the appropriate function, defined in module, to serve the request/url.
Here is the extract from the book, Pro Drupal Development, which explains the Drupal's bootstrap process,
The Bootstrap Process
Drupal bootstraps itself on every request by going through a series of bootstrap phases. These phases are defined in bootstrap.inc and proceed as described in the following sections.
Initialize Configuration
This phase populates Drupal’s internal configuration array and establishes the base URL
($base_url) of the site. The settings.php file is parsed via include_once(), and any variable or string overrides established there are applied. See the “Variable Overrides” and “String Overrides” sections of the file sites/all/default/default.settings.php for details.
Early Page Cache
In situations requiring a high level of scalability, a caching system may need to be
invoked before a database connection is even attempted. The early page cache phase lets
you include (with include()) a PHP file containing a function called page_cache_
fastpath(), which takes over and returns content to the browser. The early page cache
is enabled by setting the page_cache_fastpath variable to TRUE, and the file to be included
is defined by setting the cache_inc variable to the file’s path. See the chapter on caching
for an example.
Initialize Database
During the database phase, the type of database is determined, and an initial connection is
made that will be used for database queries.
Hostname/IP-Based Access Control
Drupal allows the banning of hosts on a per-hostname/IP address basis. In the access control
phase, a quick check is made to see if the request is coming from a banned host; if so,
access is denied.
Initialize Session Handling
Drupal takes advantage of PHP’s built-in session handling but overrides some of the handlers
with its own to implement database-backed session handling. Sessions are initialized
or reestablished in the session phase. The global $user object representing the current user
is also initialized here, though for efficiency not all properties are available (they are added by an explicit call to the user_load() function when needed).
Late Page Cache
In the late page cache phase, Drupal loads enough supporting code to determine whether or
not to serve a page from the page cache. This includes merging settings from the database into the array that was created during the initialize configuration phase and loading or parsing module code. If the session indicates that the request was issued by an anonymous user and page caching is enabled, the page is returned from the cache and execution stops.
Language Determination
At the language determination phase, Drupal’s multilingual support is initialized and a decision is made as to which language will be used to serve the current page based on site and user settings. Drupal supports several alternatives for determining language support, such as path prefix and domain-level language negotiation.
Path
At the path phase, code that handles paths and path aliasing is loaded. This phase enables
human-readable URLs to be resolved and handles internal Drupal path caching and
lookups.
Full
This phase completes the bootstrap process by loading a library of common functions, theme
support, and support for callback mapping, file handling, Unicode, PHP image toolkits, form
creation and processing, mail handling, automatically sortable tables, and result set paging. Drupal’s custom error handler is set, and all enabled modules are loaded. Finally, Drupal fires the init hook, so that modules have an opportunity to be notified before official processing of the request begins.
Once Drupal has completed bootstrapping, all components of the framework are available.
It is time to take the browser’s request and hand it off to the PHP function that will
handle it. The mapping between URLs and functions that handle them is accomplished using
a callback registry that takes care of both URL mapping and access control. Modules register
their callbacks using the menu hook (for more details, see Chapter 4).
When Drupal has determined that there exists a callback to which the URL of the browser
request successfully maps and that the user has permission to access that callback, control is handed to the callback function.
Processing a Request
The callback function does whatever work is required to process and accumulate data needed to fulfill the request. For example, if a request for content such as http://example.com/
q=node/3 is received, the URL is mapped to the function node_page_view() in node.module.
Further processing will retrieve the data for that node from the database and put it into a data structure. Then, it’s time for theming.
Theming the Data
Theming involves transforming the data that has been retrieved, manipulated, or created
into HTML (or XML or other output format). Drupal will use the theme the administrator
has selected to give the web page the correct look and feel. The resulting output is then sent to the web browser (or other HTTP client).
Eaton's answer provides a good overview. (I'm new here so I can't mod him up, thus the comment.)
The brutal "aha" moment for me was realizing everything happens through index.php, and then through the waterfall of modules (core first, then by site). To extend core functionality don't rewrite it. Instead copy the module into /sites/all/modules/ or /sites/[yoursite]/modules and extend THAT, or create a new module in those places. Same for themes. Module directories can contain display code as well, in the form of tpl, css etc.
If you're used to stricter MVC type frameworks like Rails, Django etc. all this gets a little confusing. Modules can mix in a lot of display code, and if you're looking at someone else's modules or templates you'll eventually wind up walking backwards through the stack. That's the beauty/pain of working in PHP.
Ironically, "just build an app" might be the worst way to learn this. Drupal does so much out of the box that's simply obscure until you figure out the control flow. There's nothing in a tpl file that tells you where a function with a fun name like l() comes from, for example.
It depends on how deep an understanding you're looking for; if you have a good knowledge of php, I would suggest reading through the code itself, starting with index.php, and then going on to the includes/bootstrap.inc, and then some of the other scripts in that directory.
The key include files:
menu.inc is very important to understanding how the overall system works, as it handles a lot of the implicit mapping of URLs to content.
common.inc has most of the otherwise-mysterious functions that form the basis of the API.
module.inc handles the hook invocations that Eaton mentioned
form.inc deals with form display, submission and processing
theme.inc handles presentation.
There's also some key functionality in the modules/ directory; in particular, modules/node/node.module forms the basis of the node system, which is in general what's used to encapsulate site content.
The code is, in general, very well-commented and clear. The use of Doxygen markup within the commenting means that the code effectively is the canonical documentation.
It also helps to do this using an editor that can quickly jump to the definition of a function. Using vim in combination with ctags works for me; you do have to tell ctags to index .inc, .module, etc. files as php files.
This (for Drupal 6) & this (for Drupal 7) is a pretty good architectural overview of drupal. If you want more detail then I would start writing something most of the documentation is good. Trying to learn it at a high level of detail without something concrete to achieve will be much more difficult that trying something out.
I learned loads by importing the drupal .php code into a NetBeans project.
You can then run the netbeans debugger and watch the different phases of the page come together.
The best books on the subject are "Pro Drupal Development" and "Using Drupal."
"Pro Drupal Development" includes several nice flowcharts and thorough summaries of each of Drupal's APIs (forms, theming, etc.). It's intended to be especially instructive to people making their own modules and themes, but has lots of value to the average PHP-savvy developer who wants to understand Drupal. Besides which, I've created a custom module for every site I've built, just to gain the extra control over things like selectively hiding fields on various forms (which you generally want to do for the sake of simplifying node forms for end-users), so it's good to have this knowledge under your hat.
"Using Drupal" is aimed at the site developer who wants to know how to build the good stuff like galleries, blogs, and social networking sites. It goes through several use-cases and shows how to configure existing modules to do each job. In the process it familiarizes you with the essential add-on modules "Content Construction Kit" (CCK) and "Views," how to make custom blocks and templates, and the ins-and-outs of maintaining a Drupal site. I recommend this book especially for those who want to get up to speed and actually USE Drupal right away. In the process you gain an understanding of the internal organization of Drupal.
New contributor here, 2 years late on the conversation ;-)
Replying to https://stackoverflow.com/a/1070325/1154755
To extend core functionality don't rewrite it. Instead copy the module
into /sites/all/modules/ or /sites/[yoursite]/modules and extend
THAT, or create a new module in those places. Same for themes.
Actually, I never had to copy a core module to update it. Drupal Hooks should be all you need.
For themes, yeah, sometimes it's the only way to go, but often, you can build a subtheme to get the result you need.