I am recoding my site using CodeIgniter. This is my first time using an MVC or any other type of development pattern. I am using the video tutorials off of the CI site to make a playground and get acquainted with the protocol of the system.
I've just encountered my first problem and it has to do with a parent class.
I am on the second tutorial trying to make a blog and have this:
<?php
class Test extends CI_Controller {
function Test()
{
parent::CI_Controller();
$this->load->scaffolding('entries');
}
function index()
{
$data['muck'] = 'test test test';
$data['kookoo'] = 'howdy howdy hi';
$data['hi'] = 'holla';
$data['yo'] = 'fa la la';
$data['zoom'] = '1234';
$data['array'] = array('hi','howdy','hey','sup');
$this->load->view('test_view', $data);
}
}
?>
When I loaded the page without the content inside the function Test() I noticed that the system could not find "Controller". I discovered that the tutorial is using an older version of CI and that "CI_Controller" is the proper name for the class Controller. Now with the above code I'm getting this error:
Fatal error: Call to undefined method CI_Controller::CI_Controller() in /Users/michaelsanger/Sites/CodeIgniter/application/controllers/test.php on line 7
I've scoured and am really not sure why it can't define it.
thanks in advance!
You're mixing up things from different versions, the 1.7 one focusing mainly on PHP4-style class construction (using a method with the same name as the class as constructor, instead that the dedicated magic method __construct() available in php 5)
Also, beware that scaffolding is not present in the latest versions. You didn't say which one are you using, I suppose V2. In case you're using an older version, 1) use the latest :) 2) the parent class was just Controller.
It should be like this
class Test extends CI_Controller {
function __construct()
{
parent::__construct();
//$this->load->scaffolding('entries');
}
function Test()
{
// this will call a method name test, so maps to a URL like Test/test
}
}
Note that it's not needed to extends the parent controller, unless of course you want to "autoload" a library to have it available to all methods.
CI is known for its great and easy documentation, so whenever you're using tutorial found on the net, expecially if a bit old (in internet terms), make an habit of going to the user_guide (which is also shipped along with the installation files, for local browsing) whenever you have doubts or problems.
For example, check the controllers page, you'll soon see what's wrong with your snippet (and the tutorial, as of today)
UPDATE:
In routes you're setting a route, that maps to a controller(/method).
Quoting the changelog:
Version 2.0.0
Release Date: January 28, 2011 Hg Tag: v2.0.0
General changes
PHP 4 support is removed. CodeIgniter now requires PHP 5.1.6.
Scaffolding, having been deprecated for a number of versions, has been removed.
So I don't know what you mean now with scaffolding. Looking at your route, CI expects a controller named "scaffolding_trigger", which has to be rerouted to the controller "scaffolding". If any of those are present, you get the 404 error.
Please, choose a version and stick to that, don't mix things! and don't rely on tutorials, they're not always up-to-date with the latest changes.
Related
In fatfree framework,the route can do such things instead of writing lines of codes in callback function
$f3->route('GET /about','WebPage->display');
Where WebPage->display is class method and the class will be instanced before the method is invoked,I'm thinking of ways to do the same in slim framework but I don't have a clue while reading their doc on their official website,can you help me?
You are correct, as of the time of writing there is still nothing I can find in the docs about mapping routes to classes in the Slim Framework.
But the functionality does exist, since version 2.4.0 (November 2013). It's called "Class Controllers".
The link above provides a simple example, but I will provide one as well.
You can use classes as controller class instances, as callbacks for your Slim app routes (and their parameters):
$app->get('/user/:id/', '\User:find');
This will call the find method of the User class and pass the value if the :id parameter as the first parameter to the find method. So your callback class might look something like this:
class User {
public function find($theId) {
// Do something with $theId...
}
}
It is unfortunate that this useful Slim Framework feature has not found its way to the official documentation (yet).
I am trying to upgrade my CakePHP application from 1.3.6 to 2.2.4
I did all the upgrade steps based on official CakePHP upgrade documentation.
But i am struggling with this error:
Class 'Content' not found in
C:\wamp\www\cakephp-2.2.4\app\Controller\Component\OrderBaseComponent.php
on line 20
Argument 1 passed to Component::__construct() must be an instance of
ComponentCollection, none given, called in
C:\wamp\www\cakephp-2.2.4\app\Controller\Component\OrderBaseComponent.php
on line 17 and defined [CORE\Cake\Controller\Component.php, line 77]
For the first error make sure you always App::uses() every single class you use in your code.
So your Content (whatever class it is) must also be included before you can actually use it.
Only if it is a model you can just use ClassRegistry::init(), otherwise put sth like
App::uses('Content', '[TYPE]'); at the top of the file.
This second error is pretty self-explanatory!
Look into the outlined "CORE\Cake\Controller\Component.php" file and make sure your function does have the exact same arguments in your custom component:
public function __construct(ComponentCollection $collection, $settings = array()) {
//...
}
We don't have any relevant source code but generally you might add this at the top or your component file:
First:
App::uses('Content', 'Model');
And you might have to add something like in your init or construct method:
$this->Content = ClassRegistry::init('Content');
That should solve the first issue or give a clear explanation about what goes wrong. Actually this code was likely already not correctly independent functional.
I suspect it depends on the Model to be loaded already in another piece of code that's why it worked likely. A component should work not dependent on other pieces of code so adding the App::uses, App::import etc statements makes the code work always. For example when you re-use it in your other projects.
Second:
The second issue comes really with migration issues. Check that the model extends the Component class first.
Then make sure that if you implement a custom method __construct() but also init() for example that you check whether you should add a call to the parent. For example this applies to beforeFilter controller method.
public function beforeFilter() {
parent::beforeFilter();
}
Documentation and code example from: http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
Relevant piece of documentation: http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#components
I'm facing a rather weird problem.
In one of my Kohana Modules Autoload seems to fail for just a single class, and also only on my staging box, but not on my development box.
Also, it loads all other classes just fine in the same piece of code.
So i have this class in which i call other classes:
class PayPal_AdaptivePayment extends PayPal {
public function DoAdaptivePayment($params)
{
$receiverList = new PayPal_Container_ReceiverList();
....some more other code
$result = new Paypal_Result_AdaptiveResult($response);
}
}
Both Classes PayPal_Container_ReceiverList() and Paypal_Result_AdaptiveResult() are located in folders on the level of the class i'm calling them in.
PayPal_Container_ReceiverList() lives in ./Container/receiverlist.php
Paypal_Result_AdaptiveResult() lives in ./Result/adaptiveversult.php
sofor some reason, it can't find the PayPal_Container_ReceiverList class unless i include it specifically at the top
but it can find the Paypal_Result_AdaptiveResult() class.
There's nothing really crazy going on in the class that isn't working:
class PayPal_Container_ReceiverList extends PayPal {
public $receiver = array();
public function __construct()
{
}
}
that's it.
The PayPal_Result_AdaptiveResult class is a bit more complex, and i don't think it'd make sense pasting it here.
Giving the fact that this works on one box but not the other might give me an indication that it could have to do with php instead of kohana, but i'm not sure.
on the box it works i have php 5.3.8
on the one where it doesn't i have php 5.3.2
Does anyone see anything obivous here which i'm just not seeing?
any help is greatly appreciated!
T.
There is a typo in one of the filenames (adaptiveversult.php). Also all the folders and files should be lowercase. Perhaps the system it's working on is Windows (which is case-insensitive). Basically this is the path where the classes should be:
PayPal_Container_ReceiverList in paypal/container/receiverlist.php
Paypal_Result_AdaptiveResult in paypal/result/adaptiveresult.php
Hello and thanks for reading.
I'll get straight to the point: I have a website project that I've been building using CodeIgniter 1.7.3, which I have thoroughly enjoyed using, however I have been contemplating upgrading to CI 2.0+.
I tried a straight copy, just moved my folders for controllers, models and views over to a CI 2.0 framework, but I got a 500 server error when I tried to view my pages.
After doing some investigation I discovered that all of your controllers must now use "CI_Controller" as their parent class. Also I noticed that if you want to include a constructor in your controller class that it must use the syntax "function __construct()" as its name and of the parent class. It seems that CI 2.0+ no longer supports using a constructor that has the same name as the class name, e.g. "class Blogs extends CI_controller{ function Blogs(){parent::__construct();}}" is no longer supported?
I've been reading the CI Change Log, but all I see are bug fixes, and new features, nothing about compatibility issues with older versions of CI?
Does anyone else know of any other secret little pitfalls?
Thanks,
H
CI 2.x removed all compatibility with PHP4 and also updated a number of standards to be compliant with PHP 5.3 going forward. One of these is the constructor issue you have encountered. As of PHP 5.3, the function ClassName() is no longer the constructor for a class, it is simply another function. You must explicitly declare a __construct function to perform any tasks that need to be done when a new instance of the class is created. Given this, you should see it no longer makes sense to call parent::ClassName() in your child constructor as that function would no longer be the parent's constructor.
Another pitfall I recently had to work out is how the $_GET array is now handled. In the 1.x versions, you could use query strings to pass back extra information and still use URI segments to route to controllers and functions. This is especially useful for AJAX calls where you may not always know all the parameters being sent to and from the server in a particular request. In the 2.x versions, the config.php file contains a new option, $config['allow_get_array']. This must be set to TRUE if you want to use query strings otherwise the input class clears out the $_GET array as part of CI's initialization routine on each request.
Something which isn't a pitfall but you may find useful is the new options in config/autoload.php that allows you to add new application directories to your project. If you work on a number of different projects with CI and want to keep any useful libraries you write in a single location, you can now add that location to $autoload['packages']. CI expects any path in this array to contain the sub-directories "controllers", "models", "libraries" and "helpers". It won't complain if you don't have those directories but you will at least need them for anything you intend to load, i.e. libraries would live in /libraries as with the main application folder.
Have you read the official guide for upgrading from 1.7.x to 2.x ?
so in short
Update Models and Controllers to
extend CI_Model and CI_Controller
Update Parent Constructor calls
class Wow extends CI_Controller {
function __construct()
{
parent::__construct();
//your stuff
}
function index()
{
// just for example
}
}
I'm building a library for our CodeIgniter app, but it requires many classes (currently I'm at 12).
Is there a best practice for packaging these many clients into one library. So I can just make one call to load it. i.e:
$this->load->library('soaplibrary');
Thanks!
As Summer points out, they have handled this situation somewhat elegantly in CI 2.0 with the concept of Drivers.
With a Driver, you actually create a subdirectory within your 'libraries' directory that contains your 'super' class, and another directory for 'child' classes. Better visual representation of the structure...
This was taken from Here.
and once you have constructed your library, here is the documentation on how to use it.
In CI 2.0, there are drivers to handle this situation. Good luck!
In CodeIgniter 3.1.9 when you load a library file, all classes in this file are included into code.
Let's say in soaplibrary.php you have
class SoapLibrary {
public function someMethod(...
class Test {
public function anotherMethod(...
In your controller you can do:
$this->load->library('soaplibrary');
//now on you can do
$this->soaplibrary->someMethod();
//but also
$test = new Test();
$test->anotherMethod();
CodeIgniter attempts to call the constructor of class SoapLibrary, hence a class with that name must be in there.