Anybody get the "drivers" feature working? |
As described, CodeIgniter's drivers feature is very useful, but although the code was changed from version 2 to version 3, the documentation remained the same, and it doesn't work.
http://www.codeigniter.com/user_guide/ge...ivers.html http://www.codeigniter.com/user_guide/ge...ivers.html The documentation apparently didn't work for version 2 either, and a developer called Kevin Phillips had a good tutorial here: http://www.kevinphillips.co.nz/news/code...-tutorial/ Unfortunately, that tutorial no longer works either. Does anyone have updated documentation?
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Show some code, and let us know what it is that you think is broken. I've used CI2 drivers successfully, and actually used Kevin Phillips' tutorial to get started. I'm going to try it on CI3 soon, so if it is broke, we will figure it out.
So, there is either a bug, or a documentation error that makes a big difference between CI2 and CI3.
In CI 2 the array of $valid_drivers would start with the main library name, so if your main library is Mail, and you have a swiftmailer driver, then the entry in $valid_drivers is "Mail_swiftmailer". In CI 3 the array of $valid_drivers must not include the name of the main library, so the entry in $valid_drivers would be "swiftmailer". If the docs are correct, then this is a bug, and line 106 of system/libraries/Driver should be: Code: if ( ! in_array($child_name, $this->valid_drivers)) instead of Code: if ( ! in_array($child, $this->valid_drivers)) I submitted a pull request: https://github.com/bcit-ci/CodeIgniter/pull/4314 , however, the pull request fails the tests, and I don't have time to mess around with it. For now, just adjust the valid drivers, and you should be good.
Thank you! I ran into the same thing but didn't know how to explain it succinctly. No matter what I did, it would always wind up with the wrong class name or unable to load the class. Thanks for doing the pull request. I wouldn't have been able to nail it down like you did.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
12-21-2015, 01:58 PM
(This post was last modified: 01-11-2016, 01:00 PM by RobertSF. Edit Reason: Updated information )
Ok, I got the driver library feature to work. The CodeIgniter code is fine but the documentation isn't. Here's a quick thing I wrote up.
How to Use CodeIgniter 3 Driver Libraries Introduction CodeIgniter has a "driver library" feature that lets you set up a parent class and any number of child classes and then easily access them and their methods. Suppose we have a parent class called "fruit" and child classes called "apple," "banana," and "cherry." This is like having printer drivers, where the print spooler is the same, but each printer has its own driver that interfaces the spooler to the printer. That's basically the idea here. Directory Structure and File Names Create these directories and files. Make sure you get the capitalization right, and then let's look at what goes in them. Code: /application/libraries/Fruit Note: CodeIgniter files begin with code that prevents direct access. For brevity, this code won't be included in the examples below. PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); The Parent Class PHP Code: class Fruit extends CI_Driver_Library { Our parent class extends CI_Driver_Library and then declares an array called $valid_drivers. This is required by CodeIgniter. The array holds the names of all the child classes without prefix (e.g. 'apple' not 'Fruit_apple'). How you load those values is up to you. In this example, the array is populated in the constructor when the driver is first loaded. It's also up to you how to persist those values. You can use Codeigniter's Config class, store the values in a database, or whatever you decide. Following the constructor, declare whatever methods all child classes will have in common. Code these as you normally would, and don't forget the closing curly bracket for the class itself. I always do. ![]() The Child Classes PHP Code: class Fruit_apple extends CI_Driver { Using Driver Libraries You must first load the driver library. The second argument is the array of valid driver names. CodeIgniter passes it to the parent class constructor. Again, you may load the array of valid names in some other fashion, in which case you would just load the driver. PHP Code: $this->load->driver('fruit', array('apple', 'banana', 'cherry')); Loading the driver library makes all the child classes available without further effort. For example: PHP Code: $this->fruit->apple->describe(); You can invoke driver library methods dynamically, using PHP's variable execution ability. PHP allows you to store the name of a function or method in a variable and to then execute the variable as if it were the function or method. For example, PHP Code: $my_fruit = 'banana'; CodeIgniter's Driver Libraries satisfy many use cases. For example, I'm working on an application that converts web search results to CSV data that can be loaded into Excel et al. Every search engine returns results in different formats, so in this application, the parent class has general methods that retrieve web pages and clean up fields before assignment, and the child classes have the code specific to the website they are designed to process. Using Driver Libraries, I can release this project, and people can add driver class files they create for their own purposes without having to modify any of the existing code. This tutorial is copyright 2015 by Robert Hallsey and released under GPL 3.0. I am grateful to GitHub user 'xiers' for his https://github.com/xiers/CI3_Drivers_example, written in Chinese. This is not a translation of his work but a rewrite based on it. Update 01/11/2016: Note that a controller that loads a driver library may not also have a public or protected property by the same name. That is, if you have a driver library called Fruit, the controller that loads that driver library cannot also have a public or protected $fruit. This becomes obvious once you figure it out, but until you do, it can drive you nuts wondering what the problem is.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
|
Welcome Guest, Not a member yet? Register Sign In |