unlimited parameters get last one - php

I'm using codeigniter. How can I get last part of my URL like:
www.example.com/uk-en/category/subcategory
Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"
if url is www.example.com/uk-en/category I want "category"
if url is www.example.com/uk-en I want "uk-en"
Edit
if url is www.example.com/Home/index/uk-en/category then it's working
but What i want without class name "Home" and method "index"
Like this www.example.com/uk-en/category
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
public function index($params=[]){
$params=func_get_args();
$last=end($params);
echo $last;
}
}
?>
routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['(:any)'] = 'Home/index';
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
?>
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Use this in your routs
$route['(.*)'] = "Home/index";
This to print all routs in your controller in your index function
print_r($this->uri->segment_array());

Problem in your routes is with setting wildcard placeholder on first place. It always need to be at last place.
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;
//some example routes
$route['specific/route'] = 'controller/method';
$route['specific/(:num)'] = 'page/show/$1';
//always put your wildcard route at the end of routes.php file
$route['(:any)'] = 'home/index';
Docs.

Related

Codeigniter - Routing to subfolders not working

Hello im having an issue here im new in learning codeigniter but my main problem is i cannot locate the url i want to have like for this:
I need to type http://localhost/ciHrs/admin/pages whereas i only want to get the url http://localhost/ciHrs/admin/ please kindly explain how can I access this url. and lastly the Default Controller I want to access http://localhost/ciHrs/admin/ directly. thanks
I have a subfolder name admin on controller and subfolder name admin on view and subfolder admin on model
here is my routes
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['admin/rooms'] = "admin/rooms/index";
$route['default_controller'] = 'admin/pages/view/dashboard';
$route['admin/(:any)'] = "admin/pages/view/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
my Controller to access this page
<?php
class Pages extends CI_Controller{
public function view(){
$data['title'] = 'Dashboard';
$this->load->view('admin/templates/header');
$this->load->view('admin/pages/dashboard',$data);
$this->load->view('admin/templates/footer');
}
}
Try the following rules on your routes :
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'admin/pages/view/dashboard';
$route['admin'] = "admin/pages";
$route['admin/'] = "admin/pages";
$route['admin/rooms'] = "admin/rooms/index";
$route['admin/(:any)'] = "admin/pages/view/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I think you are not relying to CI uri routing
EXPLAINING
On your routes:
$route['admin/rooms'] = "admin/rooms/index";
So here, you can call the domain and the corresponding admin/rooms depends on your $route definition. It looks like this: http://example.com/admin/rooms instead of http://example.com/admin/rooms/index
The default_controller seems to be loaded when the cookie session was set. You can only call the domain itself and it will load automatically the default_controller that you set.
Hence, on your question you said that you only want to get http://localhost/ciHrs/admin/, why not define in on $route just like this:
$route['ciHrs/admin/'] = "ciHrs/admin/pages";
or considering if the pages uri is dynamic set your route to:
$route['ciHrs/admin/(:any)'] = "ciHrs/admin/$1";
Hope this helps!

Why do I have to include index in URL CodeIgniter?

I have created a controller call posts like below
<?php
class Posts extends CI_Controller {
public function index(){
$data['title'] = 'Latest posts';
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
}
I have set routes like below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'pages/view';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'pages/view/$1';
$route['posts'] = 'posts/index';
but if I go to the url http://localhost/Blog/posts it dies with 404 page error
But if i go to http://localhost/Blog/posts/index it works fine
why I cant get it to work in the first url? what did I do wrong please help me with this i am new to codeigniter
Looks like Blog might be a subdirectory where you have your CodeIgniter installation.
CI will treat any incoming uri as /controller/action/parameters and try to instance the corresponding Controller class. If that's the case, it's trying to instance a controller named Blog and can't find it.
To fix this you have to update your config.php and set:
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Blog/';
change $route['posts'] to $route['/posts'] in router or remove that router since index method will get called as default.

Codeigniter route URL contact page

I'm new in Codeigniter I'm not sure how to use Codeigniter Routing. I've created the Contact.php in the controller folder and contact.php in the views folder.
In routes.php I have put $route['Contact'] = 'controller/contact'; but when I enter the url http://mytest.dev/contact/ it shows 404 Page Not Found. The page you requested was not found.
I want when I enter "http://mytest.dev/contact" it will show the contact page
Thanks in advance.
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('contact')
}
}
In CI there is an index.php in the URL ( by default ). So, you can access your page with this url http://mytest.dev/index.php/contact
For removing it from URL and have it like you want you need to add .htaccess file in your project directory
Check this answer for it
Also, you don't need to change your routes.php every time after creating a new page. Leave it like this
$route['default_controller'] = 'welcome'; // or contact
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I see you are using $route['Contact'] = 'controller/contact';
Tell us your controller's class name and which function/method did you use from the above you are referencing to contact() and that controller name doesn't make sense. You route will normally be in lowercase too.
If you named your class Contact (which seems like it) then you need to put a .htaccess file in the folder where your index.php or base_url resides (or root directory) and then remove the value in application/config.php as $config['index_page'] = ''; so that you can access it from http://mytest.dev/contact
To make it clearer. The format should be $route['AAA'] = 'BBB/CCC';
AAA is the url path of your choice
BBB is the name of your controller's class
CCC is the function/method of the page you want to show
If you didn't add the htaccess, you must put /index.php/ before your preferred path.

Codeigniter: 404 on existing controller

When I set $route['default_controller'] to 'blog', CI opens my controller without problems. When I want to access it via URL, CI returns a 404.
My controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Saved at
application/controllers/Blog.php
Some other config things:
$route['default_controller'] = 'blog';
$route['404_override'] = '';
$config['base_url'] = '';
URL I try to use:
http://localhost:63342/project/public_html/blog/
.htaccess to hide index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
How can I fix this issue? I tried a lot of solutions from similar questions without success
If your project is in project folder in main directory of the local server, change your address to:
http://localhost:63342/project/blog
You configured that page as your default controller, so it should work even by:
http://localhost:63342/project

URI multilingual CodeIgniter

We're using Codeingiter library i18n (link) to create a multilingual site.
Before this, we had for example www.thedomain.com/register and register was a function in our controller. Now when we put this library it grabs the domain, the language string, the controller's name and the functions name: www.thedomain.com/es/homegf/register (where homegf is our controller).
We want this URI's to work without the name of our controller on it (www.thdomain.com/es/register) like in the librarie's examples but we think the problem is in our routes.php.
This is what we have in routes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "homegf";
$route['404_override'] = '';
$route['^(en|es|de)/(.+)$'] = "$2";
$route['^(en|es|de)$'] = $route['default_controller'];
This is our .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|files|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can find our code to review it at https://bitbucket.org/ticketcomunicacion/grinfood/src/17ddde60e340a1f2bc389f54ec579e1e903ee86b?at=multilenguaje
as i understand , you want the controller url without controller name .
if you want to change the URL from your controller name to other ,
in your routes.php write the following
$route['homegf/register'] = "register";
and you can access
domain.com/es/register
The problem was in our routes.
This is how we managed to make it work:
$route['^es/(.+)$'] = "homegf/$1";
$route['^en/(.+)$'] = "homegf/$1";
$route['^es$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
$route['(:any)'] = 'homegf/$1';

Categories