Codeigniter 4 Tutorials
- Codeigniter 4
- Application Run
- Application Structure
- Configuration files
- Errors
- Controller
- View
- Model
- How to remove index.php from URL
- Route
- CodeIgniter URLs
- Helper
- Session
- Global Functions and Constants
- Logging Information in CI4
- Error Handling
- Signin / SignUp
- Login / Register
- CRUD
- Crud 1
- Form Validation
- Data Table
- Pagination
- File Upload
- Show Data Using Ajax
- Select2 AJAX Autocomplete Search
- Curl Post
- Rest API
- Weblink
What is Codeigniter Controller?
Codeigniter controller is a simple class file. The first letter of a controller class name must be capital. A Controller receives the user input and perform the action on the user input and validates it, and then passes the input to the Model. It performs interaction on the model objects.
Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.
File name : Itechxpert.php
<?php
namespace App\Controllers;
class Itechxpert extends BaseController
{
public function index()
{
echo 'Hello Itechxpert.in!';
}
}
the index() method method is always loaded by default if the second segment of the URI is empty.
The second segment of the URI determines which method in the controller gets called.
File name : Itechxpert.php
<?php
namespace App\Controllers;
class Itechxpert extends BaseController
{
public function index()
{
echo 'Hello Sana Habib!';
}
public function show_birthday()
{
echo '07th, May-2020';
}
}
Now load the given URL and see the show_birthday method:
itechxpert.in/index.php/itechxpert/show_birthday/
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
File name : Itechxpert.php
itechxpert.in/products/men/sandals/123
<?php
namespace App\Controllers;
class Products extends BaseController
{
public function men($sandals, $id)
{
echo $sandals;
echo $id;
}
}
Defining a Default Controller
CodeIgniter 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.
File name : Routes.php
To specify a default controller open your app/Config/Routes.php file and set this variable:
$routes->setDefaultController('Home');
Where ‘Home’ is the name of the controller class you want to be used.
A few lines further down Routes.php in the “Route Definitions” section comment out the line:
$routes->get('/', 'Home::index');
Organizing Your Controllers into Sub-directories
create sub-directories under the main app/Controllers/ one and place your controller classes within them.
File name : Itechxpert.php
Folder names MUST start with an uppercase letter and ONLY the first character can be uppercase
Controller Function :-
File name : Itechxpert.php
public function view($page = 'home')
{
if ( ! is_file(APPPATH.'/Views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
}
$data['title'] = ucfirst($page); // Capitalize the first letter
echo view('templates/header', $data);
echo view('pages/'.$page, $data);
echo view('templates/footer', $data);
}
Methods
In the above example, the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty.
The second segment of the URI determines which method in the controller gets called.
File name : Itechxpert.php
<?php
namespace App\Controllers;
class Itechxpert extends BaseController
{
public function index()
{
echo 'Hello Sana!';
}
public function dob()
{
echo '7th May, 2020';
}
}
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
ittutorial.in/itechxpert/dob/sana/7
Your method will be passed URI segments 2 and 3 (“sana” and “7”):
File name : Itechxpert.php
<?php
namespace App\Controllers;
class Itechxpert extends BaseController
{
public function dob($sana, $id)
{
echo $sana;
echo $id;
}
}
Defining a Default Controller
CodeIgniter 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 app/Config/Routes.php file and set this variable:
File name : Itechxpert.php
$routes->setDefaultController('Itechxpert');
Private methods
In some cases, you may want certain methods hidden from public access. To achieve this, simply declare the method as private or protected.
File name : Itechxpert.php
protected function utility()
{
// some code
}
File name : Itechxpert.php
File name : Itechxpert.php
File name : Itechxpert.php
File name : Itechxpert.php
File name : Itechxpert.php
File name : Itechxpert.php
File name : Itechxpert.php