Controllers
Controllers are the heart of your application, as they determine how HTTP requests should be handled.
- NipIgniter Controller
- Hello World
- Functions
- Passing URI Segments to Your Functions
- Defining a Default Controller
- Remapping Function Calls
NipIgniter Controller
A NipIgniter Controller has little difference with CodeIgniter Controller. But, the concept is not different at all. The Differences is the class name, file name, and the URL. The controller also extends to Nip_Controller class.
Consider this URI:
example.com/blog/
In the above example, NipIgniter would attempt to find a controller named BlogController.php and load it.
When a controller's name matches the first segment of a URI, it will be loaded.
Let's try it: Hello World!
Let's create a simple controller so you can see it in action. Using your text editor, create a file called BlogController.php, and put the following code in it:
Then save the file to your application/controllers/ folder.
Now visit the your site using a URL similar to this:
example.com/blog/
If you did it right, you should see Hello World!.
Note: Class names must use a camelcase letter. For example:
Class Name : BlogController URL : example.com/blog
Class Name : UserStatusController URL : example.com/user-status
Class Name : FooBarFooController URL : example.com/foo-bar-foo
Also, always make sure your controller extends the Nip_Controller class so that it can inherit all its functions.
Functions
In the above example the function name is index(). The "index" function is always loaded by default if the second segment of the URI is empty. Another way to show your "Hello World" message would be this:
example.com/blog/index/
The second segment of the URI determines which function in the controller gets called.
Let's try it. Add a new function to your controller:
Passing URI Segments to your Functions
If your URI contains more then two segments they will be passed to your function as parameters.
For example, lets say you have a URI like this:
example.com/products/some-shoes/sandals/123
Your function will be passed URI segments 3 and 4 ("sandals" and "123"):
<?php
class ProductsController extends Nip_Controller {
public function someShoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
?>
Defining a Default Controller
NipIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'blog/some-function';
Remapping Function Calls
_remap() function is used to create login feature. You can look at the file in the application/core/Nip_Controller.php
Function Reference
$this->render($string = "view_name", $data = array(), $boolean = FALSE);
Permits you to load view using a layout. It's another way to load view. We use this function to load view instead of using $this->load->view(). There is no difference in parameter.
class BlogController extends Nip_Controller{
public $pageLayout = "layout/main";
public function someView(){
$data['title'] = "With Layout";
$this->render("some-view", $data);
}
}
$this->renderPartial($string = "view_name", $data = array(), $boolean = FALSE);
Permits you to load view without a layout. The result is the same with $this->load->view().
class BlogController extends Nip_Controller{
public function someView(){
$data['title'] = "Without Layout";
$this->renderPartial("some-view", $data);
}
}
$this->paginate($baseUrl, $totalRow, $limitPerPage, $uri, $queryString = "");
Permits you to render pagination string based on some configuration. Example :
class BlogController extends Nip_Controller{
public function listing($offset = 0){
$uri = 3;
$limitPerPage = 10;
$baseUrl = site_url("blog/listing");
$article = $this->Article->all(array("limit"=>$limitPerPage, "offset"=>$offset));
$totalRow = $this->Article->count();
$exampleQueryString = "sort=date&direction=asc";
echo $this->paginate($baseUrl, $totalRow, $limitPerPage, $uri, $exampleQueryString);
}
}
Query string will produce link like this :
example.com/blog/listing/10?sort=date&direction=asc
$this->createThumb($path, $width = 400, $height = 400);
Permits you to create thumb based on image path. This function will produce thumbnail image. Example :
class BlogController extends Nip_Controller{
public function update(){
$path = "./public/uploads/someimage.jpg";
$this->createThumb($path, 500, 300); // will produce '/public/uploads/someimage_thumb.jpg'
}
}