Trending October 2023 # Codeigniter Routes: Url Routing With Example # Suggested November 2023 # Top 18 Popular | Vibergotobrazil.com

Trending October 2023 # Codeigniter Routes: Url Routing With Example # Suggested November 2023 # Top 18 Popular

You are reading the article Codeigniter Routes: Url Routing With Example updated in October 2023 on the website Vibergotobrazil.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Codeigniter Routes: Url Routing With Example

What are CodeIgniter Routes?

Routes are responsible for responding to URL requests. Routing matches the URL to the pre-defined routes. If no route match is found then, CodeIgniter throws a page not found an exception.

Routes in CodeIgniter are defined using the below formula:

HERE,

Controller -is mapped to the controller name that should respond to the URL.

Method – is mapped to the method in the controller that should respond to the URI request.

Parameter – this section is optional.

In this CodeIgniter Routes tutorial, you will learn:

CodeIgniter Routes Example

Let’s now look at a practical URL Routing in CodeIgniter example.

HERE,

The name of the controller responding to the above URL is “contacts”

The method in the controller class Contacts is “edit”

The edit method accepts a parameter. In the case of our example, the value “1” is passed to the method.

Here is a brief background of what we plan to do:

Routing – routing is responsible for responding to URL requests. CodeIgniter Routing matches the URL to the pre-defined routes. If not route match is found then CodeIgniter throws a page not found exception.

Controllers – routes are linked to controllers. Controllers glue the models and views together. The request for data / business logic from the model and return the results via the views presentation. Once a URL has been matched to a Route in CodeIgniter, it is forwarded to a controller public function that interacts with the data source, business logic and returns the view that displays the results.

Views – views are responsible for presentation. A view is usually a combination of HTML, CSS and JavaScript. This is the part is responsible for displaying the web page to the user. Typically, the data displayed is usually retrieved from the database or any other available data sources.

To learn how to implement routers on a real-world project, we will assume that we are creating an application for managing contact details. The following table shows the URLs that will be working with.

S/N URL Route Controller Method

1 / $route[‘default_controller’] Welcome index

2 /contacts $route[‘contacts’] Contacts index

3 /contacts/create $route[‘create’] Contacts create

4 /contacts/edit/id $route[‘edit/:id’] Contacts edit

5 /contacts/update/id $route[‘update/:id’] Contacts update

6 /contacts/delete/id $route[‘delete/:id’] Contacts delete

We will create the routes of our application based on the above table. We have defined the URLs, CodeIgniter route, and mapped them to the respective controller and method names.

Creating URL Routing for the Application

Let’s create CodeIgniter URL Routing for our tutorial project

Open application/config/routes.php

Modify the routes to match the following

$route['default_controller'] = 'welcome'; $route['contacts'] = 'contacts'; $route['create'] = 'contacts/create'; $route['edit/:id'] = 'contacts/edit'; $route['update/:id'] = 'contacts/update'; $route['delete/:id'] = 'contacts/delete'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE;

HERE,

$route[‘default_controller’] = ‘welcome’; defines the default controller Welcome.

$route[‘contacts’] = ‘contacts’; defines a contacts route which calls the index method in the Contacts controller

$route[‘create’] = ‘contacts/create’; defines a route create which points to the Contacts controller and calls the create method.

$route[‘edit/:id’] = ‘contacts/edit’; defines a route edit which accepts a parameter of id and points to the edit method of Contacts controller

$route[‘update/:id’] = ‘contacts/update’; defines a route update which accepts a parameter of id and points to the update method of the Contacts class.

$route[‘delete/:id’] = ‘contacts/delete’; defines a route delete which accepts a parameter of id and points to the delete method of the Contacts controller.

The following table shows the respective URLs derived from the above defined routes

S/N Route Corresponding URL

1 $route[‘default_controller’] = ‘welcome’; http://localhost:3000

2 $route[‘contacts’] = ‘contacts’; http://localhost:3000/contacts

3 $route[‘create’] = ‘contacts/create’; http://localhost:3000/contacts/create

4 $route[‘edit/:id’] = ‘contacts/edit’; http://localhost:3000/contacts/edit/1

5 $route[‘update/:id’] = ‘contacts/update’; http://localhost:3000/contacts/update/1

6 $route[‘delete/:id’] = ‘contacts/delete’; http://localhost:3000/contacts/delete/1

Now that we have covered the routes let’s create the Contacts controller that will be responding to the actions specified in the routes.

Create a new Route file in CodeIgniter as chúng tôi in application/controllers/Contacts.php

Add the following code

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Contacts extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { } public function create() { } public function edit($id) { } public function update($id) { } public function delete($id) { } }

HERE,

Class contacts extends CI_Controller {..} defines our controller class and extends the CI_Controller class which comes with CodeIgniter.

The methods defined above correspond to the routes we defined and those with parameters like delete accept a parameter of $id

Notice the functions load three (3) views. The header and footer are common for all methods. The middle view is very specific to the action, i.e. delete for delete function create a view for creating a function, etc. Another important thing to remember is that the views are loaded from the contacts subdirectory.

CodeIgniter Views

We still need to take one more step before we can test our CodeIgniter Routes with Parameters in the web browser. Let’s create the corresponding views to the above controller methods.

The following image shows what your application will look like

Create the following files in application/views

header.php – this file will contain contacts app menu and the header

footer.php – this files will contain the application footer.

Create a new directory of contacts in views application/views/contacts

Create the following files inside

index.php create.php edit.php

Your file structure should be as follows

Let’s now update the header.php

General Settings

HERE,

The following is the code for the footer.php

Let’s now add the code for the chúng tôi chúng tôi and chúng tôi files for contacts.

index.php edit.php create.php

You can save all the changes that have been made.

Open the following URL in your web browser

Summary

Routes in CI are responsible for responding to URL requests. Routing matches the URL to the pre-defined routes. If no CodeIgniter Route match is found then, CodeIgniter throws a page not found an exception.

CI Routing is responsible for responding to URL requests. Routing matches the URL to the pre-defined routes.

Controllers glue the models and views together. The request for data / business logic from the model and return the results via the views presentation.

Views are responsible for presentation. A view is usually a combination of HTML, CSS and JavaScript.

In this tutorial, we have learned how to create Routes in CodeIgniter for a real-world example application and covered the basics of routing that you need to know to get started developing CodeIgniter.

You're reading Codeigniter Routes: Url Routing With Example

Update the detailed information about Codeigniter Routes: Url Routing With Example on the Vibergotobrazil.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!