I'm beginner in codeigniter and i try obtain seo friendly url for may project.
This url domain[.]com/blog/category/subcategory/ i want to go here controllers/Blog.php->index()
i want redirect this url domain.com/blog/index/category/subcategory/ to 404
I have this controller
PHP Code:
class Blog extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$config['base_url'] = 'https://domain.com';
define('BASE_URL', $config['base_url']);
define('SITE_URL', BASE_URL . $_SERVER['REQUEST_URI']);
if ( strpos(str_replace('://', '', SITE_URL), '//') OR substr(SITE_URL, -1) !== '/' ) {
header('Location: ' . preg_replace('/([^:\/])(\/+)/','$1/', SITE_URL . '/'), TRUE, 301);
exit;
}
$url = trim($_SERVER['REDIRECT_URL'], '/');
$result = $this->db->query('SELECT * FROM blog WHERE url=$url LIMIT 1')->result_array();
if ( $result['type'] == 'category' ) {
$this->_category();
} else {
$this->_post();
}
}
function _category() {
//load category view
}
function _post() {
//load post view
}
function ajax_method() {
//method apelate with ajax
}
}
I tryed this routes but don't work ( i can't appelate this domain[.]com/blog/ajax_method or other methods from controllers/Blog.php )
Method 1
$route['blog'] = 'blog/index';
$route['blog/.*'] = 'blog/index';
Method 2
$route['blog/(:any)/(:any)'] = 'blog/index/$1/$2'; ( with this method i can domain[.]com/blog/category/subcategory/ but i can't have this url domain[.]com/blog/category/subcategory/filter/brand_dell/color_red/ )
Method 3
PHP Code:
$config['base_url'] = 'https://domain.com';
define('BASE_URL', $config['base_url']);
define('SITE_URL', BASE_URL . $_SERVER['REQUEST_URI']);
//route.php
//table routes (id, route_id, sefurl, controller, method)
require_once(BASEPATH . 'database/DB.php');
$db =& DB();
if ( strpos(str_replace('://', '', SITE_URL), '//') OR substr(SITE_URL, -1) !== '/' ) {
header('Location: ' . preg_replace('/([^:\/])(\/+)/','$1/', SITE_URL . '/'), TRUE, 301);
exit;
}
$route['default_controller'] = 'blog';
$route['404_override'] = '';
if ( count($this->uri->segments) > 0 ) {
$sefurl = trim($_SERVER['REDIRECT_URL'], '/');
$query = $db->get_where('routes', ['sefurl' => $sefurl], 1);
$result = $query->result();
if ( count($result) == 1 ) {
foreach( $result as $row ) {
$route[$row->sefurl] = $row->folder . '/' . $row->controller . '/' . $row->method . '/' . $row->route_id . '/';
}
} elseif ( count($this->uri->segments) > 1 ) {
$query = $db->get_where('routes', [
'folder' => $this->uri->segment(1),
'controller' => $this->uri->segment(2)
], 1);
$result = $query->result();
if ( count($result) == 1 ) {
foreach( $result as $row ) {
$route[$row->folder . '/' . $row->controller . ''] = 'error404';
$route[$row->folder . '/' . $row->controller . '/.*'] = 'error404';
}
}
}
}
$route['admin'] = 'admin/dashboard';
$route['translate_uri_dashes'] = FALSE;
This method work but is no pro because
this link domain.com/blog/category/subcategory => blog.php->index() and have this ['config'][uri][rsegments] = array('blog','index')
this link domain.com/blog/ajax_method => blog.php->ajax_method() and have this ['config'][uri][rsegments] = array('blog','index') and here i need ['config'][uri][rsegments] = array('blog','ajax_method') i can change rsegments values in _remap but is no pro
$route['blog'] = 'blog/index';
$route['blog/.*'] = 'blog/index';
PHP Code:
function _remap() {
if ( array_key_exists(2, $this->uri->segments) ) {
$method = $this->uri->segment(2);
//recomanded prefix $method = 'xyz_' . $this->uri->segments(2);
if ( method_exists($this, $method) ) {
call_user_func([$this, $method]);
return false;
}
}
call_user_func([$this, 'index']);
return false;
}