Codeigniter Tutorials
- What is codeigniter?
- Application_Architecture
- MVC Architecture
- HMVC Architecture
- Codeigniter Configuration
- Remove index.php from url in codeigniter
- MVC Concept
- View
- Alternate PHP Syntax for View Files
- Routing
- Codeigniter URL
- Get Current URL
- Previous page URL get
- Seo Friendly URL
- Slug Create in codeigniter
- What is _remap() function
- Remove controller name from url in codeigniter
- Codeigniter Controller Class
- Class Constructor
- GET $ POST method in Codeigniter
- Models
- Basepath, Apppath, FCPATH
- URI Segment
- Page Redirect
- Helper class
- Custom Helper class
- Form Helper
- Common Helper Functions
- Common Function
- Array Problems
- Call controller in Helper
- Add active class to menu using Helper class
- Custom Library
- Custom Library Example
- when to use get_instance()
- Codeigniter Hook
- how to work inline css in codeigniter
- Custom 404 page
- 404 custom error page
- Create custom config file in codeigniter
- How to set and get config item value
- How to Speed Up CodeIgniter App?
- Codeigniter Functions
- Session
- cookies
- How to Set & Get Tempdata in Codeigniter
- flash messages in Codeigniter
- Flashdata
- Encryption and Decryption In CodeIgniter
- Codeigniter security
- csrf token form security
- Password Hashing
- Form Validation
- Custom Validation
- Registration Form with validation
- Server Side Form Validation
- Validate Select Option Field
- Date Format Validation
- Date Format change in codeigniter
- Date Functions
- DOB Validation
- CI CRUD
- User SignUp
- User Login
- User Logout
- Login Account
- Login form with RememberMe
- Login Form with session
- User change password
- Change Password with Callback Validation to Check Old Password
- Forgot password
- Reset password
- Insert data in database
- Fetch data from database
- Update data in database
- Delete data in database
- File Upload
- Image Upload with resize Image
- Upload Multiple file and images
- Upload Multiple images with CRUD
- File and image update
- Upload Image Using Ajax.
- Email Send
- Email Send Using Email library
- Email Send Using SMTP Gmail
- Notification send
- store data in json format in DB
- Json parse
- Fetch data Using Ajax with Json data
- How to Show data Using Ajax with Json parse
- Get JSON Data from PHP Script using jQuery Ajax
- Insert data Using Ajax
- Submit data Using Ajax with form validation
- How to show data Using Ajax in codeigniter
- Insert & Update Using Ajax
- Registration Form With Validation Using Ajax in codeigniter
- Delete data Using Ajax Confirmation
- Delete All data Using checkbox selection
- Ajax CSRF Token
- Ajax Post
- Ajax serverside form validation
- Contact form using AJAX with form validation
- DataTable Using Ajax dynamically
- DataTables pagination using AJAX with Custom filter
- DataTables AJAX Pagination with Search and Sort in codeigniter
- DataTables in Codeigniter using Ajax
- Ajax Custom Serarch
- Ajax Live Data Search using Jquery PHP MySql
- Ajax Custom Serarch and sorting in datatable
- Dynamic Search Using Ajax
- Autocomplete using jquery ajax
- Jquery Ajax Autocomplete Search using Typeahead
- Dynamic Dependent Dropdown Using Ajax
- Dynamic Dependent Dropdown list Using Ajax
- Dynamic Dependent Dropdown in codeigniter using Ajax
- ajax username/email availability check using JQuery
- Check Email Availability Using Ajax
- Data Load on mouse scroll
- Ajax CI Pagination
- Pagination in codeigniter
- Ajax Codeigniter Pagination
- email exists or not using ajax with json
- CRUD using AJAX With Modal popup in CI
- Add / Show Data on modal popup using Ajax
- Modal popup Validation using Ajax
- Data show on Modal popup Using Ajax
- Add / Remove text field dynamically using jquery ajax
- How to Add/Delete Multiple HTML Rows using JavaScript
- Delete Multiple Rows using Checkbox
- Multiple Checkbox value
- Form submit using jquery Example
- REST & SOAP API
- Multi-Language implementation in CodeIgniter
- How to pass multiple array in view
- Captcha
- create zip file and download
- PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
- data export in excel sheet
- Excel File generate in Codeigniter using PHPExcel
- Dompdf library
- tcpdf library
- Html table to Excel & docs download
- CI Database Query
- Database Query
- SQL Injection Prevention
- Auth Model
- Join Mysql
- Tree View in dropdown option list
- OTP Integration in codeigniter
- curl post
- download file using curl
- Sweet Alert
- Sweet alert Delete & Success
- Log Message in Codeigniter
- Menu & Submenu show dynamically
- Set Default value in input box
- Cron Jobs
- Stored Procedure
- Display Loading Image when AJAX call is in Progress
- Send SMS
- IP Address
- Codeigniter Tutorialspoint
- Website Link
- How To Create Dynamic Xml Sitemap In Codeigniter
- Paypal Payment Integration
- Get Latitude and Longitude From Address in Codeigniter Using google map API
- How To Create Simple Bar Chart In Codeigniter Using AmCharts?
- dynamic Highcharts in Codeigniter
- Barcode in Codeigniter
- Codeigniter Interview Questions
- Project
What is Routing?
CodeIgniter has user-friendly URI routing system.
there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
http://example.com/[controller-class]/[controller-method]/[arguments]
itechtuto.com/class/function/id/
Note :- In some situations, you may want to change this default routing mechanism. CodeIgniter provides facility through which you can set your own routing rules.
Routing rules.
Routing rules are defined in your application/config/routes.php file. You will find an array called $route in which you can customize your routing rules. The key in the $route array will decide what to route and the value will decide where to route. There are three reserved routes in CodeIgniter.
Routes can be customized by wildcards or by using regular expressions but keep in mind that these customized rules for routing must come after the reserved rules.
Wildcards
We can use two wildcard characters as explained below −
Example
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Note : Here 'home' is the controller class Name.
Example
Open the routing file located at application/config/routes.php and add the following two lines. Remove all other code that sets any element in the $route array.
$route['default_controller'] = 'login/view';
$route['(:any)'] = 'pages/view/$1';
CodeIgniter reads its routing rules from top to bottom and routes the request to the first matching rule. Each rule is a regular expression (left-side) mapped to a controller and method name separated by slashes (right-side). When a request comes in, CodeIgniter looks for the first match, and calls the appropriate controller and method, possibly with arguments.
Example
$route['default_controller'] = 'home';
$route['admin'] = 'admin/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['login/(:any)']= 'admin/login';
Here admin is the directory created under the controller. and login is the controller name under the admin directory.
In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. In the above example, if the literal word "product" is found in the first segment of the URL, and a number is found in the second segment, the "catalog" class and the "product_lookup" method are instead used.
Note : You can match literal values or you can use two wildcard types: (:num) will match a segment containing only numbers. (:any) will match a segment containing any character (except for ‘/’, which is the segment delimiter). Wildcards are actually aliases for regular expressions, with :any being translated to [^/]+ and :num to [0-9]+, respectively. Note Routes will run in the order they are defined. Higher routes will always take precedence over lower ones. Note Route rules are not filters! Setting a rule of e.g. ‘foo/bar/(:num)’ will not prevent controller Foo and method bar to be called with a non-numeric value if that is a valid route.
Examples
Here are a few routing examples:
$route['journals'] = 'blogs';
A URL containing the word “journals” in the first segment will be remapped to the “blogs” class.
$route['blog/joe'] = 'blogs/users/34';
A URL containing the segments blog/joe will be remapped to the “blogs” class and the “users” method. The ID will be set to “34”.
$route['product/(:any)'] = 'catalog/product_lookup';
A URL with "product" as the first segment, it is the sub directory and anything in the second will be remapped to the "catalog" class and the "product_lookup" method.
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
A URL with “product” as the first segment and it the directory, and a number in the second will be remapped to the “catalog” class and the “product_lookup_by_id” method passing in the match as a variable to the method.
Regular Expressions
$route['products/([a-z]+)/(\d+)']='$1/id_$2'; In the above example, a URI similar to products/shoes/123 would instead call the “shoes” controller class and the “id_123” method.
Reserved Routes
There are three reserved routes:
$route['default_controller'] = 'welcome';
This route points to the action that should be executed if the URI contains no data, which will be the case when people load your root URL. The setting accepts a controller/method value and index() would be the default method if you don’t specify one. In the above example, it is Welcome::index() that would be called.
$route['404_override'] = '';
This route indicates which controller class should be loaded if the requested controller is not found. It will override the default 404 error page. Same per-directory rules as with ‘default_controller’ apply here as well.
It won’t affect to the show_404() function, which will continue loading the default error_404.php file at application/views/errors/error_404.php.
$route['translate_uri_dashes'] = FALSE;
this is not exactly a route. This option enables you to automatically replace dashes (‘-‘) with underscores in the controller and method URI segments, thus saving you additional route entries if you need to do that. This is required, because the dash isn’t a valid class or method name character and would cause a fatal error if you try to use it.
Note :- The reserved routes must come before any wildcard or regular expression routes.
URL SEO Friendly in codigniter
Removing index.php from url using .htaccess
RewriteEngine on
RewriteCond $1! ^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Default controller Change
$route['default_controller'] = 'welcome';
change to
$route['default_controller'] = 'your_controller_name'
URI Routing
// it hide the controller name from url.
$route['default_controller'] = "home";
$route['aboutus'] = "home/about";
$route['service'] = "home/service";
$route['contactus'] = "home/contact";
you write the link your on view page.such as
<a href="<?php echo base_url()?>aboutus"></a>
<a href="<?php echo base_url()?>contactus"></a>
it redirect to home/about controller
Dynamic URL's Route
(:num) - will match a segment containing only numbers.
(:any) - will match a segment containing any character.
$route['news'] = "category/news/1";
$route['bollywood'] = "category/bollywood/2";
Dynamic Product URL
Example category url
trendeelook.com/mens/t-shirt/
trendeelook.com/mens/shirt/
trendeelook.com/mens/trousers/
rounting code would be
$route['mens-clothing/(:any)'] = "mens/clothing/$1";
Example product urls
trendeelook.com/mens-fit-shirt-sale/
trendeelook.com/woman-fit-shirt-sale/
trendeelook.com/kids-fit-shirt-sale/
rounding code should be
$route['default_controller'] = "product";
$route['(:any)'] = "product/index/$1";
Adding a URL Suffix
$config['url_suffix'] = '.html';
Routing Example
$default_controller = 'home';
$route['default_controller'] = $default_controller;
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['admin'] = 'admin/login';
//$route['login/(:any)']= 'admin/login';
$route['news/(:any)']= 'news/postdetails/$1';
$route['post/(:any)']= 'post/postdetails/$1';
$route['bollywood']= 'category/bollywood/$1';
$route['login-form'] = "Login";
$route['admin-dashboard'] = "admin/dashboard";
$route['news']= 'category/news/1';
$route['chutkule']= 'category/chutkule/2';
$route['sports']= 'category/sports/3';
$route['health']= 'category/bollywood/6';
$route['education']= 'category/bollywood/7';
$route['bollywood']= 'category/bollywood/8';
$route['photogallery']= 'category/photogallery/9';
$route['fashion']= 'category/fashion/10';
//$route['news'] = "post/postdetails";
//$route['(:any)/postdetails'] = "post/postdetails";
//$route['post/postdetails/(:any)'] = "post/postdetails";
// $route['p/(:any)'] = 'web/p/$1';
// $route['ajax-form-validation/post']['post'] = "AjaxFormValidation/validationForm";
/*
$route['home.html'] = "home";
$route['aboutus.html'] = "home/aboutus";
$route['services.html'] = "home/services";
$route['portfolio.html'] = "home/portfolio";
$route['blog.html'] = "home/blog";
$route['howitwork.html'] = "home/howitwork";
$route['contactus.html'] = "home/contactus";
*/
/*
// it hide the controller name
$route['default_controller'] = "home";
$route['about'] = "home/about";
$route['service'] = "home/service";
$route['contact'] = "home/contact";
*/
/*
// Setting Dynamic URL's Route
$route['blog/new-topic-list'] = "blog/index/1";
$route['blog/old-topic-list'] = "blog/index/12";
*/
/*
$route['overseas/detail/(:any).html']="overseas/detail/$1/$1";
$route['make-payment.html']="online_payment/index/$1/$1";
$route['payment-verification.html']="online_payment/make_payment/$1/$1";
$route['default_controller'] = "home";
$route['courses/detail/(:any).html']="courses/detail/$1/$1";
$route['courses/(:any).html']="courses/sub_course/$1/$1";
$route['admin'] = "admin/login";
$route['contact.html'] = "home/contact";
$route['404_override'] = 'notfound';
*/
/*
// Dynamic Product URL
example.com/mens-clothing/t-shirt/
example.com/mens-clothing/shirt/
example.com/mens-clothing/trousers/
$route['mens-clothing/(:any)'] = "mens/clothing/$1";
$route['default_controller'] = "product";
$route['(:any)'] = "product/index/$1";
*/
/*
// Adding a URL Suffix
$config['url_suffix'] = '.html';
*/
Custom url Routing
$route['home'] = 'HomeController/index';
$route['about-us'] = 'HomeController/add_aboutus';
$route['service'] = 'HomeController/show_services_list';
$route['photo-gallery'] = 'HomeController/photo_gallery_show';
$route['news-events'] = 'NewsController/show_news_events';
$route['contact-us'] = 'ContactusController/add_contactus';
<a href="<?php echo base_url()?>home"> Home</a>
<a href="<?php echo base_url()?>about-us"> AboutUs</a>
<a href="<?php echo base_url()?>service"> Services</a>
<a href="<?php echo base_url()?>photo-gallery"> Gallery</a>
<a href="<?php echo base_url()?>news-events"> News & Events</a>
<a href="<?php echo base_url()?>contact-us"> ContactUs</a>