I currently test my CodeIgniter app with phpunit by using CIUnit (https://bitbucket.org/kenjis/my-ciunit). The problem is that I have multiple controllers with the same name. I have a controller in the root controller directory named "Blog" and I have a controller called "Blog" in the controller/ajax/ directory.
The reason is to seperate all ajax requests from the main controller.
When I am running tests on both files, I get the following error:
PHP Fatal error: Cannot redeclare class Blog in ...
Well, I am not suprised I am getting this error.
What are my options to resolve this?
Prefix controllers in ajax directory with "ajax" (looks only a bit stupid url/ajax/ajax_blog)
Use namespaces (I guess I need to namespace codeigniter too then)
Create 3 seperate phpunit.xml files
This aren't really solutions I am looking for. Do I have any other options? Is it somehow possible to run each testsuite seperatly, but still in one command? Can I "clean" objects between testsuites? Anything else?
There are no other options except those you mentioned, as it is impossible to "unload" class definitions in PHP.
Naming two controllers the same is not a problem when you run CI normally, since only one controller is instantiated per request, but something that should be avoided.
If it is just the ajax-url you don't like, maybe override it in a route (in config/routes.php):
$routes['ajax/blog'] = 'ajax/ajax_blog';
Related
I've been testing out the Imagine Library in my Laravel application and have been running the code straight in the Routes file which has worked fine. However, now when I'm trying to use it in a Model or Controller, it can't seem to find the library anymore even though I've included it the same way I did before with use Imagine\Image\Box; and use Imagine\Gd;. The code is also exactly the same as in the Routes file but I still get class 'App\Imagine\Gd\Imagine' not found.
Do I have to do something different when including stuff in a Model or controller?
I have an iPhone application which perform calls to an API developed with codeigniter. Since I am new to the framework, I want to know where such API are normally located ? The url requested from the iPhone application to the API is the following:
http://example.com/site/welcome/admin/api/processingdocuments/
I got a look on the application/libraries folder and there is a bench of php files like rest.php and REST_Controller.php. Are these the files I am looking for? Since the URL above doesn't point to specific file, how to know which API is being called? Thank you in advance.
When using Phil Sturgeon's CodeIgniter Restserver (which is what it seems you are using), you set up a controller within application/controllers directory and extend his class.
Let's say you call your controller Foobar, and it looks like this:
class Foobar extends Rest_Controller {
//... your methods here
}
In this case, you will access this endpoint at http://example.com/foobar
The URL references your controllers, and not the Rest_Controller directly.
URLs look like this:
http://example.com/{controller}/{method}/{param1}/{param2}/.../{paramN}
If method is not specified, it defaults to index()
Further, the Restserver allows you to map methods to HTTP request methods. Such that
GET example.com/foobar will map to the method index_get()
POST example.com/foobar will map to the method index_post()
..and so on.
I highly recommend you read the documentation
In your example, you would expect the controller that is being called to be located at application/controllers/site.
However, this might have been modified using htaccess rewrite rules (check for a .htaccess file), or via redefined CI routes (check application/config/routes.php).
If all else fails:
Under default configurations, that is where you should find them, however, CI is very malleable with routes and it is hard to say where they might be. Your best bet would be to grep the directory for the words extends Rest_Controller since wherever the controllers are, they would be extending that class.
I have a controller that is only built in Debug (using #if DEBUG) that I use to house some test methods that I don't want on the production server.
The problem I'm running into is that using T4MVC I get a derived class based on the controller with overloads of all the methods. As soon as I try to build in Release mode that base class and virtual action methods are no longer present and I get build errors.
Ideally I would like to be able to exclude the controller from T4MVC but I can't see a way to do that in the settings file and the answer to this question: T4MVC How to Exclude Individual Files suggests its not possible.
Anyone know of a way to do it or can think of a good work around?
Use the T4MVC Attribute on your controller
[T4MVC(false)]
public Controller ControllerToIgnore //...
Worked for my purposes (to exclude a specific controller from being generated).
From the T4MVC documentation
Consider separating your test methods and controllers into a separate project that depends on your production code. This way you would not need to have #if Debug stuff. I'm not sure what is your set up, but you can try using NonActionAttribute that is wrapped inside #if !Debug clause. So your production code would not run the methods as actions on your controllers. But this stinks with the wrong layout of the project.
I am working on a project where I create Two Application hosted in same site.
My structure is given below…
SITE
SYSTEM
APPLICATION
font_end
back_end
Now my question is,is it possible to access model of one application from another application.
As example, I have a model named ‘User_model’ in font_end application. Is it possible to use this model from back_end application.
Thanks.
Yes, it is possible, but there is a but. It doesn't matter where your files are in an absolute sense, but it is not necessarily the easiest thing in the world to accomplish.
Your best bet is to use symlinks if you can and link them into a sub-directory of your models directory. This would be simple and clean.
Barring that, you should extend Loader and overwrite the &model method to look in the secondary directory (perhaps reassign $path to the alternate application's model folder if $path == 'frontend').
If that also isn't an option, loading is done through APPPATH.'models/'.$path . '/' .$model.EXT. This means you can access the model by the relative path to APPPATH.'models/'. Don't do that if you can possibly avoid it, however. It is non-obvious and an invitation to errors.
I tried your last version (error prone I know) and got this result:
Unable to locate the model you have specified: ext.
I used this load code to access the frontend model from my backend:
$this->load->model('APPPATH.'/models/frontend/'Frontend_Model'.'EXT');
apppath and ext constants should be used like variables, but if I put it this way my notepad ++ highlighting goes wrong:
$this->load->model(APPPATH.'/models/hp/'Homepage_Model'.EXT)
admin/application/model/accounts_model.php
application/controller/home.php
Put this code in home.php to use model of admin applicaton
$this->load->model('../../../Unicorn/application/models/accounts_model');
I am working on a new module of existing live project. It is a website developed in PHP Zend Framework. New module is on admin side. It runs properly on my localhost/virtualhost.
When I uploaded it online with correct directory path it is found that one file is not found.
It is called like
www.example.com/admin/controllerName/actionName
All the actions works except one action that doesn't display anything and returns exception error mentioning that page or file not found.
What could be the issue? I have checked folders. If one action for the same controller works than why doesn't other. Both have their .phtml files in View section.
Help would be appreciated.
Would I be right if I guessed your local development environment was Windows?
It's probably a filename case sensitivity issue.
Assuming your request looks like
www.example.com/admin/fusionmaps/newpage
I'm not sure how you've setup your modules but if it's reasonably standard, you should have something like this (note the upper and lowercase characters)
application/modules/admin/controllers/FusionmapsController.php
The controller classname should be Admin_FusionmapsController with action public function newpageAction()
The view script should be at
application/modules/admin/views/scripts/fusionmaps/newpage.phtml