How to create first application in ci 4

Deepak Tailor Image
Deepak Tailor - Sep 18 2020
How to create first application in ci 4

First of all start coding for debugging. There is an env file in your project to start debugging. Rename this file as .env. Then set the CI_ENVIRONMENT by default ENVIRONMENT production is set. If you set it to production mode, then changes in the production file or changes in the development file.

Then run the command.

php spark serve

After running this command, it return a localhost URL like “locahost:8080”

// use namespace for all controllers files
namespace App\Controllers;

// load codeigniter controller class
use CodeIgniter\Controller;

class Home extends Controller
{
	public function index()
	{
		echo "call index function";
	}

	public function multiple_page_show()
	{
		echo "call multiple page show function";
	}
}
How to load view file in controller
// use namespace for all controllers files
namespace App\Controllers;

// load codeigniter controller class
use CodeIgniter\Controller;

class Home extends Controller
{
 public function index()
{
 return view('welcome_message');
}

public function multiple_page_show()
{
 echo view('header');  // this is header.php file
 echo view('main_page');  // this is main_page.php file
 echo view('footer');  // this is footer.php file
}
}
Use error exceptions in controller
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('header', $data);
   echo view('pages/'.$page, $data);
   echo view('footer', $data);
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in