Routes in codeigniter 4 with parameters - deesoft service

Routes in codeigniter 4 with parameters

Deepak Tailor Image
Deepak Tailor - Sep 27 2020
Routes in codeigniter 4 with parameters

Optimize your urls in CodeIgniter 4 with our comprehensive guide on utilizing routes and parameters. Create dynamic and customizable URLs for your applications efficiently. URL of codeigniter follows string class, method, argument pattern. It contains the first segment class name, this is our controller file. And the second segment is the method name. And after this, whatever is passed are all arguments.

Domain.com/class/method/argument/argument/argument/argument
---------------------------
example.com/product/1/
example.com/product/2/
example.com/product/3/
example.com/product/4/

In this example, the function is named instead of the class and has passed the argument. But the method is passed in the second segment. To create such a URL, custom routes have to be set.

Setting our custom routing rules :

All the custom routing rules that you have to set are in the application / config / routes.php file.

url example: domain.com/custom-url
// set routes :
$routes->get(‘/custom-url’,’Home::function_name’);
How to set placeholders

We can access dynamic URL values from placeholders. For example, if the URL has a name or numeric number, we can set the rules on it.

$routes->get('/custom-url/(:num)/(:any)','home::custom_url/$1/$2');
$routes->get('/blogs/(:any)','home::blog_detail/$1');
http://localhost/first_ci4_project/custom-url&254785/Deepak%20Tailor
$routes->get('/custom-url&(:num)/(:any)','home::custom_url/$1/$2');
How to set Redirecting routes

First of all, we create a root, define it as a name like ‘about’. Then redirect this route to another route.

$routes->add('home/about-us','Home::about_us',['as'=>'about']);
$routes->addRedirect('home/contact-us','about');
$routes->addRedirect('home/service','about');
How to create group in routes
$routes->group('students',function($routes){
	$routes->add('index/(:any)','Users::index/$1');
	$routes->add('login','Users::login');
	$routes->add('register','Users::register');
});

// how to call url in browser
// domain.com/students/index/123
// domain.com/students/login
// domain.com/students/register

$routes->group('user',function($routes){
	$routes->add('login','Users::login');
	$routes->add('console','Users::register');
});
// how to call url in browser
// domain.com/user/login
// domain.com/user/console

In Roots we have been given some global options by Codeigniter. We set the rules by doing them.

$routes->add('from', 'to', $options);
$routes->get('from', 'to', $options);
$routes->post('from', 'to', $options);
$routes->put('from', 'to', $options);
$routes->head('from', 'to', $options);
$routes->options('from', 'to', $options);
$routes->delete('from', 'to', $options);
$routes->patch('from', 'to', $options);
$routes->match(['get', 'put'], 'from', 'to', $options);
$routes->resource('photos', $options);
$routes->map($array, $options);
$routes->group('name', $options, function());
How to use dash in urls

In codeigniter, we can also create SEO friendly URLs. By using desh in the URL, for this we have to true setTranslateURIDashs() function in the routes.php file.

$routes->setTranslateURIDashes(true);
How to use codeigniter 404 override

We use this function for page not found. In this function, the name of the controller and the function has to be passed. If the user searches any page and that page is not found, then we get him to show the message of Page Not Found.

$routes->set404Override(function(){
	echo view('welcome_message');
});
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