How to get Controller, Action, URL informations with CodeIgniter

I have these urls:

How to get controller name, action name from these urls. I’m codeigniter newbie. Are there any helper function to get this info.

You could use the URI Class:

$this->uri->segment(n); // n=1 for controller, n=2 for method, etc

There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, we could use

 $controllerName = $this->uri->segment(1, 'magazine');  

I’ve also been told that the following work, but am currently unable to test:

$this->router->fetch_class();
$this->router->fetch_method();

CakePHP 2.0 Naming Conventions

The PHP’s popular Framework has some Naming conventions that need to be followed at the time of development.

Controller Conventions

Controller class names are plural, CamelCased, and end in Controller and the filename would be ‘ApplesController.php’.

e.g. PeopleController and LatestArticlesController

File and Class Name Conventions

In general, filenames match the class names, which are CamelCased. So if you have a class MyNiftyClass, then in CakePHP, the file should be named MyNiftyClass.php. Below are examples of how to name the file for each of the different types of classes you would typically use in a CakePHP application:

  • The Controller class KissesAndHugsController would be found in a file namedKissesAndHugsController.php
  • The Component class MyHandyComponent would be found in a file named MyHandyComponent.php
  • The Model class OptionValue would be found in a file named OptionValue.php
  • The Behavior class EspeciallyFunkableBehavior would be found in a file namedEspeciallyFunkableBehavior.php
  • The View class SuperSimpleView would be found in a file named SuperSimpleView.php
  • The Helper class BestEverHelper would be found in a file named BestEverHelper.php

Each file would be located in the appropriate folder in your app folder. Continue reading