Codeigniter Controller

Create controller in codeigniter

<?php
namespace App\Controllers;

class UserController extends BaseController{
    public function create(){

        echo "Create method";

  }
}

Note:

Controller name must start with Capital Letter UserController

 

Create controller within Subdirectory .

Controllers > Blog > CategoryController.php

Passing parameters to controller methods

<?php
namespace App\Controllers;
class UserController extends BaseController{
    public function create(){
        echo "Create method";
    }

    public function user($name="Guest", $age=0){
        echo "You are logined as $name and $age year's old.";
  }
}
http://localhost:8080/usercontroller/user

As you can notice in above line username and user age does not pass there after user method, so default arguments Guest as username and 0 age will be used.

http://localhost:8080/usercontroller/user/Rama/20

But as you can notice in above line we pass "Rama" as username and age is 20

© 2016 - 2022, All Rights are Reserved.