php CodeIgniter framework try to use camelCase - php

im newbie in code igniter, im okay with the underscore convention, but i want to make my own libraries/extended core CI classes/custom function with camelCase convention.
i tried to change :
$config['subclass_prefix'] = 'My';
and the create :
class MyController
class MyModel
myFunction
and it works, but as the documentation said that i shouldn't use camelCase convention.. I just use camelCase so that i know which is the CI's core classess/functions and which is my custom.
until now(3 days with CI) it doesn't produce any trouble with CI and im enjoying this, but yeah im just newbie in CI if anyone have/had bad experience using camelCase with CI please let me know..
the question is:
is it okay for me to use camelCase for custom class/function in CI? i mean will it become trouble in the CI?
CI version 2.X
PHP 5.4

If you’re not a fan of CodeIgniter naming convention tyranny and want to use CamelCase or lowerCamelCase file names do this
Make a file called path/to/app/application/core/My_Loader.php
In the file make a class like this
<?php
class My_Loader extends CI_Loader
{
}
Find the CI_Loader class in path/to/app/system/core/Loader.php
Copy method public function model
Paste it into My_Loader
Delete $model = strtolower($model); and $model = ucfirst($model); from the My_Loader::model method.
Shazam!

Related

Adding PHP namespaces into an existing ZF1 application

I'm trying to integrate PHP namespaces into an existing Zend Framework project (v1.12). When I add namespacing at the top of a working controller, it doesn't work anymore and the application throws an Invalid controller class error. Here's my controller definition :
namespace MyProject\Controller;
use MyProject\Controller\MyRestController;
class MyFooController extends MyRestController
{
}
and the init method within the Bootstrap.php:
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyProject');
return $autoloader;
}
Just a guess (have not used ZF for quite some time): Zend will not accept any class as a controller, just those extended from the framework's base controller class. As you don't extend from the frameworks base controller class you see the error.
If that is the reason, take care you initially extended from the base framework controller class or you implemented the needed interface.
namespace MyProject\Controller;
class MyRestController extendes Zend_Framework_Base_Controller_Class_Name_Here
{
...
p.s. the use MyProject\Controller\MyRestController; looks superfluous as that class is in that namespace already. Let's review your code:
namespace MyProject\Controller;
This sets the namespace of the file. That means, non-FQCN will resolve into it. For example:
new MyRestController();
Resolves to the following FQCN:
new MyProject\Controller\MyRestController
Which - oha! - is exactly what you wrote in use:
use MyProject\Controller\MyRestController;
Which means, that this use clause is superfluous, the extend in:
class MyFooController extends MyRestController
Would go to it anyway at first. Because it's the same namespace.
I am facing similar problem now. For me this looks like that Zend cannot properly resolve namespaced controller name. So when I put for example IndexController into namespace \Basic\Controller, it will be not loaded because Zend want to load \IndexController class, which does not exist.
I am thinking about extending standard zend router class, which has method getControllerName.
Then I can set this in bootstrap by:
$router = new \My\Namespaced\Router();
$front = Zend_Controller_Front::getInstance();
$front->setRouter($router);
I didn't tried that code yet but this should work.

call libraries from model in codeigniter

I'm trying to call a library from model in codeigniter but doesn't want to fire and I don't really know how to debug it, I have been trying to use codeigniters default logging method but doesn't show anything. My code looks as it follows.
class Model_products extends CI_Model{
function __construct(){
parent::__construct();
$this->load->library('ApiClient');
log_message('error', $this->load->library('ApiClient'));
}
}
/application/libraries/ApiClient.php
libary final class ApiClient
{}
You cannot name a library ApiClient - class names should follow the CI naming convention (which is identical to ucfirst()) and when loading anything in CI you need to load it with lowercase naming.
Naming is the key.
Your file should be named Apiclient.php (libraries has the first letter in uppercase in the filenames, models, views and controller are all lowercase filenaming).
Your class definition should reflect this also:
class Apiclient
{
...
}
And when using the CI instance to load the library, you need to load it with lowercase naming:
$this->load->library('apiclient');
You miss a semicolon at the end of your line $this->load->library('ApiClient'). Could this be it?

How can I use my own model class in the Zend Framework?

I wrote a DB class and I want to create my own model class in the Zend Framework. My own model extends from PDO and it automatically creates a query to work with the database.
How can I use my own model class in the Zend Framework?
Lets say you put your class in the models/Myclass.php file then your class would look something like this:-
class Application_Model_Myclass
{
//all my class stuff goes here
}
You could then use it in your controller or in another model like this:-
$myclass = new Application_Model_Myclass();
There is more information on Zend Framework naming conventions in the manual.
Zend Framework has no official model support, so you don't need to do anything special. As long as your model has methods for saving, updating, and deleting, you can use it as you would any other model.
You won't have access to any of the Zend Framework "automagic", such as automatically populating fields. You'll have to expose public methods for getting and setting all of your fields.

CakePHP and namespaces?

Is there a way to put your own code into namespaces using cakephp? The following very simple controller class works fine.
class Customer extends \AppModel {
var $name = 'Customer';
}
However, if I add
namespace foo\bar;
cakephp can't find the controller anymore. Is there some way to tell cake in which namespace it should look for controllers?
I am using cakephp 1.3 and php 5.3.
I don't think there is. CakePHP looks for classes like PostsController or BlogController, not foo\bar\PostsController. Maybe you can tell CakePHP in what folder to look for those classes (probably), but then it will still be looking for unnamepsaced class names.
Why would you want this in a framework that doesn't use namespaces?
Why not give up the App::import() in cakephp 1.3. Replace it with the include_once().
I got my customize vendor classes defined under a namespace works fine. Just to prevent the collision of the custom class name with the official one.

Load models in Zend Framework

I tried to search here before creating this, but I couldn't find anything.
I have a simple project without modules and I'd like to load my models (which are inside application/models) without using any namespace and without usign any extra loading lines.
Basically what I want to do is to have my class Projects extends Zend_Db_Table_Abstract inside my models folder and to load it in my controller using $db = new Projects();
Is there anyway to do this? Is it recommended to use Model_Projects instead?
What If I had modules?
Edit:
I tried to use this without any other implementation and I got Class 'Projects' not found
It is because the Projects class is not autoloaded. Depending on your application namespace, (for example the default namespace 'Default') you have to name your class into something like: Default_Model_Projects

Categories