Handle URL in CodeIgniter - codeigniter

I have a url like
"www.site.com/index.php/details/productname"
and I get 404 error on that link in codeigniter but the URL www.site.com/index.php/details/ is working fine. I want handle url parameter in seo manner.

You have to add /index/ segment in part of your url:
http://www.site.com/index.php/details/index/productname/
If you want to open url such like that http://www.site.com/index.php/details/productname/:
1) you need to define _remap() method:
public function _remap($method)
{
if ($method == 'index')
{
$this->viewDetails($this->uri->segment(2));
}
else
{
$this->default_method();
}
}
OR
2) use application/config/routes.php file
$route['details/(:any)'] = "products/viewDetails/$1";
From User-Guide:
routers.php
_remap method

Related

Laravel + Inertia: switch rootView layout when redirecting

I'm using Laravel + Inertia + React in my project and I define the rootView layout in the HandleInertiaRequests middleware, depending on which route is requested, like this:
public function rootView(Request $request)
{
if ($request->route()->getPrefix() == "/admin") {
return "adm";
} else {
return "app";
}
}
But I found a problem when redirecting from a route with app rootView to a route with adm rootView (from /login to /admin).
The problem is that the rootView is not changed before or after the redirect. Is there a way to force the redirection to reload the full page?
I found the solution in this thread: https://stackoverflow.com/a/68609938/173299. The trick is add the following to the login request in AuthenticatedSessionController:
if ($request->session()->has("url.intended")) {
return Inertia::location(session("url.intended"));
}
I'm leaving my original question just in case anyone else find it useful.

How to remove controller name and function name in codeigniter

I am creating a directory site in codeigniter. On landing page load it prompts to select state then city. Then it redirects from www.example.com to www.example.com/location/city-name/.
Here Site is the controller name and location is the function name within it.
I have already removed controller name from the url using route.php. Code is below:
$route['location/(:any)'] = 'site/location';
But I want to remove function name location too from the url and url should be www.example.com/city-name/.
Further when category is selected after selecting the city it should be redirected to www.example.com/city-name/category-name/ but I am able to redirect to www.example.com/location/city-name/category-name/ only using following lines in route.php:
$route['location/(:any)/(:any)'] = 'site/location';
I can capture city and category name using $this->uri->segment and use the same for filter process there is absolutely no issue with it.
But main issue remains to remove the function name location.
I have searched suggested questions before posting this question but could't find any suitable solution. Can anyone help me to achieve the goal?
My site controller is below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('file');
$this->load->model('query_model');
$this->load->library("pagination");
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
}
public function index(){
$this->load->view('website/index');
}
public function location(){
$this->load->view('website/index');
}
}
I think I am doing some mistake either in index or location function. Location Function is nothing just showing the view of index. Index view has the code to grab uri segment and filter the data accordingly.
My code which is passing state id to a jquery function:
<a onclick="get_cities(2)">Andaman & Nicobar Islands</a>
My jquery code get_cities(id) is below:
function get_cities(id){
var aj_url="<?= base_url();?>ajax_post_controller/show_cities";
jQuery.ajax({
type: "POST",
url: aj_url,
data: {id: id,},
success: function(res, status) {
if (res)
{
alert(res);
$("#loc-head").html("Select City");
$("#loc-data").html(res);
$(".modal-footer").css("display","block");
}
}
});
}
After defining below code:
$route['(:any)'] = 'site';
$route['(:any)/(:any)'] = 'site';
Alert from get_cities function returning html code of index page and Index page is displayed in opened model too.
If I comment above two lines I get the list of cities it returns the url of city
Visakhapatnam
But due to above 2 route lines commented above city url give an error of 404.
hope this will help you :
$route['location'] = 'site/index'; /* redirect to index method */
$route['location/(:any)'] = 'site/location'; /* redirect to location*/
$route['location/(:any)/(:any)'] = 'site/location/$1';
add like this
In routes.php
$route['location/(.*)'] = 'site/index';
In Controller
public function index(){
if($this->uri->segment(1) && $this->uri->segment(2)){
$this->load->view('website/category');
}elseif($this->uri->segment(1)){
$this->load->view('website/city');
}else{
$this->load->view('website/index');
}
}

Codeigniter - url segment replace or redirect

I have a base controller (base) which all other controllers extend from.
Anything placed here will override other controllers, the redirects will be here.
URLs example:
http://domain.com/controllerone/function
http://domain.com/controllertwo/function
http://domain.com/controllerthree/function
Using the code below. will give me the controller name
$this->uri->segment(1);
Each of the above controllers need to be redirected to separate URLs, but the funcation part should not change:
http://domain.com/newcontrollerone/function
http://domain.com/newcontrollertwo/function
http://domain.com/newcontrollerthree/function
In my base controller i want the following logic:
$controller_name = $this->uri->segment(1);
if($controller_name === 'controllerone'){
// replace the controller name with new one and redirect, how ?
}else if($controller_name === 'controllertwo'){
// replace the controller name with new one and redirect, how ?
}else{
// continue as normal
}
i was thinking i should use redirect() function and str_replace(), but dont know how efficient these would be. Ideally i do not want to use the Routing class.
thanks.
try
header("Location:".base_url("newcontroller/".$this->uri->segment(2)));
Simple Solution using segment_array:
$segs = $this->uri->segment_array();
if($segs[1] === 'controllerone'){
$segs[1] = "newcontroller";
redirect($segs);
}else if($segs[1] === 'controllertwo'){
$segs[1] = "newcontroller2";
redirect($segs);
}else{
// continue as normal
}
CodeIgniter's URI Routing, should be able to help in this case. However, if you have a good reason not to use it, then this solution may help.
The potential redirects are in an array, where the key is the controller name being looked for in the URL and the value is the name of the controller to redirect to. This may not be the most efficient but I think it should be easier to manage and read than a potentially very long if-then-else statement.
//Get the controller name from the URL
$controller_name = $this->uri->segment(1);
//Alternative: $controller_name = $this->router->fetch_class();
//List of redirects
$redirects = array(
"controllerone" => "newcontrollerone",
"controllertwo" => "newcontrollertwo",
//...add more redirects here
);
//If a redirect exists for the controller
if (array_key_exists($controller_name, $redirects))
{
//Controller to redirect to
$redirect_controller = $redirects[$controller_name];
//Create string to pass to redirect
$redirect_segments = '/'
. $redirect_controller
. substr($this->uri->uri_string(), strlen($controller_name)); //Function, parameters etc. to append (removes the original controller name)
redirect($redirect_segments, 'refresh');
}
else
{
//Do what you want...
}

CodeIgniter - dynamic pages - default controller url

edit
my solution in routes.php:
$route['news'] = 'news_controller';
$route['gallery'] = 'gallery_controller';
$route['(:any)'] = 'sites/$1';
and in my site conroller:
function index($site_id = '') {
//sanitize $site_id.
$this->site = $this->sites_model->get_site($site_id);
//etc.
}
THX to YAN
question:
so i wrote a little CMS with CodeIgniter. The admin can create sites. the site opens automatically when the segment of the url is like one in the DB. eg mysite.com/sites/about will call the "About" site. this works fine.
now i got a problem with my URL. i want this url
http://www.mysite.com/sites/about
turns to this:
http://www.mysite.com/about
the problem is, that i cannot use the routes.php and set wildcards for each site. (because they are dynamic and i dont know wich site the customer will create - and i dont want to edit the routes.php file for each site he will create - this should be done automatically)
the problem is i got other fix controllers too, like news, gallery or contact:
mysite.com/news, mysite.com/gallery, ...they work fine
so here is my Site Controller:
class Sites extends Public_Controller {
public $site;
public $url_segment;
public function _remap($method)
{
$this->url_segment = $this->uri->segment(2);
$this->load->model('sites_model');
$this->site = $this->sites_model->get_site($this->url_segment);
if($this->site !== FALSE)
{
$this->show_site($this->site);
}
else
{
show_404($this->url_segment);
}
}
public function show_site($data)
{
$this->template->set('site', FALSE);
$this->template->set('site_title', $data['name']);
$this->template->set('content',$data['content']);
$this->template->load('public/template','sites/sites_view', $data);
}}
and this is the Site_model who checks the database...if the url_segment fits the title in the DB:
class Sites_model extends CI_Model {
public function get_site($site_url)
{
if($site_url != ""){
$this->db->where('name', $site_url);
$query = $this->db->get('sites', 1);
if($query->num_rows() > 0){
return $query->row_array();
}
else
{
return FALSE;
}
}else{
return FALSE;
}
} }
i think i need something who checks if the controller exists (the first segment of the url) when not call the Site controller and check if the site is in the DB and when this is false then call 404.
any suggestions how this can be solved?
btw: sry for my english
regards GN
You can handle routes.php in the following way, just keep the (:any) value last:
$route['news'] = 'news_controller';
$route['gallery'] = 'gallery_controller';
$route['(:any)'] = 'sites/$1';
In your sites controller route to the specific site using the data from the URL.
function index($site_id = '') {
//sanitize $site_id.
$this->site = $this->sites_model->get_site($site_id);
//etc.
}
I am having trouble understanding the full intent of the question, but, from what I can tell, don't you simply need to add:
if ($this->uri->segment(1) != 'sites')
... // handle malformed URL

Why my code igniter controller works partially?

I have enabled the query string settings and works fine for all the query string related URL's but except for this login controller... when I try like this, it gives me an error,
404 Page Not Found
The page you requested was not found.
URL : www.test.com/login/?sType=1
but when I access without query string, www.test.com/login/ ... works fine...
similarly other query string urls working fine as below,
http://www.test.com/core/child/register/?sIds=3,4,&type=1
etc.,
How can I fix this issue?
My login controller,
function Login()
{
parent::Controller();
$this->load->model("user_login_model");
$this->load->library('user_agent');
}
// to load the login screen
function index()
{
//$this->load->model("user_login_model");
$this->load->view('login/user_login_form');
}
// to show as login
function show()
{
$this->load->view('login/user_login_form');
}
// to process the login
function checkuser()
{
}
// to logout
function logout()
{
}
}
/* End of file welcome.php /
/ Location: ./system/application/controllers/welcome.php */
I have also experienced some problems in the past when there is only one element in query string. Try this:
www.test.com/login/?sType=1&n=1
It might work.

Resources