Can you add a prefix to controller file names in Codeigniter? - php

I like to name my files with a prefix so that when I have them open in an editor I can distinguish what kind of file I'm working with easily.
So instead of naming the controller file home.php or account.php, I want to be able to add a prefix to the file name like controller.home.php.
Is there a configuration option in Codeigniter that let's you do this?

No, there isn't such an option, there's no practical reason for it to exist.
Controllers typically extend the CI_Controller class, so that helps you know what type of class you're editing, if that is of any use to you.

There is no configuration in code-egniter, i guess.
You can not use "." in name, instead of this you can use naming convention like
"controller_home.php" or "controller-home.php"
"controller_account.php"
May be this will help.

in this case, you need rewrite CI router according to your logic

Related

Create folder/Sub-folder in Code-igniter

I am a Drupal dev and new to code-igniter or any such php frameworks.
Now i have to modify an existing application done on codeigniter and the structure must be as follows:
example.com/motors
example.com/motors/car-for-sale
example.com/motors/car-for-rent etc.
Before it has only one url example.com/motors and i want to create more urls as mentioned above.
In the application\views\content folder i have the following structure:
application\views\content\motors.php
application\views\content\motors
application\views\content\motors\car-for-sale.php
In the application\controller folder i have the following structure:
application\controller\motors.php
application\controller\motors\motors.php
application\controller\motors\car-for-sale.php
I want to get the url example.com/motors & example.com/motors/car-for-sale from the files resides in the motors folder.Also how can i set a default file to load when i open example.com/motors?
You can't have a (controllers) directory that matches the name of a controller class at the same level. That is, since you have a controllers/motors.php, the files under controllers/motors/* will never be reached.
Instead (and this is the answer to your second question), you should set the default_controller name and rename controllers/motors.php to controllers/motors/<default_controller>.php.
Note that the default_controller setting points to a controller name (not a file location) and is applied to all directories. That is, if you set it to 'Default', then controllers/Default.php will be used when you open http://domain.tld/ and controllers/motors/Default.php will be used if you open http://domain.tld/motors/.
Also, your controller names MUST start with a capital letter, so default.php would be incorrect and should be Default.php instead. This might be working for you on Windows right now (because of its case-insensitive file system), but as soon as you upload your site to a Linux (or other UNIX-based) host, any classes with file names that don't start with a capital letter won't work.
It looks like you're trying to build a CodeIgniter site with a completely different paradigm from what it is designed around.
The structure you are after can be set up using the routes.php file within application/config
In there, you can set routes to go to any location needed, so for you, something like:
$routes['motors/cars-for-sale'] => 'motors/cars_for_sale';
$routes['motors/cars-for-rent'] => 'motors/cars_for_rent';
Then in application/controller you'd have a Motors.php file, which starts:
class Motors extends CI_Controller{
And also has the functions cars_for_sale and cars_for_rent
The mappings in routes sets this to link together.
In order to get the views you want for any given route, in the controller function, you'd have:
$this->load->view('path/to/view/file', $array_of_data); // view path does not need the .php extension
I'd recommend having a look and possibly even a follow through of the CodeIgniter tutorial in their documentation

How to use variable from routes.php in controller

In CakePHP have a bunch of unique URL names redirected in routes.php file.
Similar to this:
$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0],
array('controller' => 'Foo','action' => 'bar',3,60));
I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.
How can I reach a variable in routes.php from a controller?
So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.
I'm not cakephp user but simple search shows that there is class called ClassRegistry.
You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.
Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.
You can save key-value pairs with
Configure::write('key','value');
and read it again with
Configure::read('key');

change configure variable dynamically cakephp

Let's say I have this in my bootstrap.php file in a cakePHP application:
Configure::write('Config.language', 'eng');
How can I dynamically change the value of that configuration variable based on user action in a controller of my application? I tried to do the same thing that I did above here in my controller, but that didn't work
Any help?
Try Configure::write('Config.language', 'dut'); for e.g.
This answer from the question suggested by #Ryan Pendleton shows a somewhat correct way to use this directive.
It should be used in the AppController because it gets loaded first - as the parent of all other controllers in the application itself.
I used "somewhat correct" because it is best to validate the language codes ('eng', 'fre', 'dut') in the app/config/routes.php file - go here for more information.
Also do check out this: Internationalization-Localization explanation.

Cakephp controller name

i m new to cakephp. i have stuck in one place the problem is
table name - india_tends
controller - tends
model name - india_tends
The problem is that cake php not allow tends to controller name its show that i have to use india_tends.. so any way to give tends to controller name?
use the variable $useTable="india_tends" and $name="tends" in model
If your table name is india_tends, then your controller code should be in the file india_tends_controller.php, and the class name should be IndiaTendsController.
In your case I think you want the IndiaTendsController to be accessible through /tends/:actions so the best guess would be using routing.
Edit your config/routes.php to have the following code:
Router::connect('/tends/*', array('controller' => 'india_tends'));
And you can have it accessible through /tends/*
Which version of CakePHP are you using?
You could use routing.

How to Extend a Helper in CodeIgniter?

I want to add some new functions to the core string helper, which is found in system/helpers folder. I think there was a 'right' way to do this using MY_String_helper, or something of that sort. I can't remember exactly how it was done though. Any thoughts on this issue?
I found it. Make a file with a name such as this, in the application/helpers directory:
MY_xx_helper.php
E.g:
MY_string_helper.php
Then you can call
$this->load->helper('string');
And it should load all the existing helper functions as well as the new ones you add.
Doing that you can not only add new functions but replace exising helper functions.
For a primary source, in case things change in the future, the CodeIgniter User Guide's Helpers page has a section describing how to extend helpers.

Categories