CI Controller

What is a Controller?

In CodeIgniter, a Controller is a PHP class that acts as the middleman between the user (browser) and your application logic.

It receives requests from the browser, processes them (using models if needed), and then loads the appropriate view.

Controller Structure

  • Controllers are stored in:
  • application/controllers/ (CodeIgniter 3)
  • app/Controllers/ (CodeIgniter 4)
  • File name must match the class name.
  • The class name should start with a capital letter.


Example
<?php
namespace App\Controllers;

class Home extends BaseController
{
public function index()
{
return view('welcome_message', [
'message' => 'Welcome to CodeIgniter 4!'
]);
}
}

Passing Data to Views

Example
public function about()
{
return view('about_view', [
'message' => 'About Us Page'
]);
}

Controller Action Return Types

1. Normal Website Pages (Views)


return view('page_view', $data);


2. Redirect to Another Route


return redirect()->to('/dashboard');


 3. Return JSON (API)


public function getData()
{
  return $this->response->setJSON(['a', 'b', 'c']);
}



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.