How to remove controller name and function name in codeigniter - 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');
}
}

Related

laravel route issue .the page you are looking for could not be found

I want to show data from the database when I press the button in one view it displays me the specific data by the id in the other view this is my first view.
In my controller:
public function show($id) {
$symptoms=symptoms::all();
foreach ($symptoms as $symptom) $symptom=symptoms::find($id);
return view('front.second',compact('symptom'));
}
My problem is when displays the second view it can't find the page and the route just display the id not the method/id :(
Try this:
public function show(symptoms $symptom)
{
return view('front.second', compact('symptom'));
}
Or:
public function show($id)
{
$symptom = symptoms::find($id);
return view('front.second', compact('symptom'));
}
And read Laravel's Controllers Documentation carefully.
As you shared image, is clearly shows that you are using below route
{{route('show',['id'=>111])}}
Check your route by php artisan route:list and use right one, I guess you should use like
{{route('user.show',['id'=>111])}}
If you Controller name is UserController in resource

How to maintain url in codeigniter for pages,categories,subcategories and product?

I'm trying to do URL format likes below
for pages -
www.example.com/page-name
for categories
www.example.com/category-name/sub-category-name
for product
www.example.com/category-name/sub-category-name/product-name
or
www.example.com/product-name
is this possible ? if yes mean how should be my controllers and functions ? should I any changes in routes.php ?
I have solution for your problem :)
1) If you use HMVC (https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/) that determine priority for example (example module: front):
page_name (controller: Page)
product_name (controller: Product)
categories (controller: Categories)
next in routes:
$route['(:any)'] = function ($slug)
{
return 'front/page/show/'.$slug;
};
next check in controller Page check exists there is a link. If not exists that run next controller Product. for example:
...
public function show($slug='')
{
$this->load->model('page_m');
$page = $this->page_m->GetRowBySlug($slug)->row();
if(!$page) {
echo modules::run('front/product/show', $slug);
exit;
}
...
next in controller Product add to same but if not exists link run next controller etc:
...
public function show($slug='')
{
$this->load->model('product_m');
$page = $this->product_m->GetRowBySlug($slug)->row();
if(!$page) {
echo modules::run('front/categories/show', $slug);
exit;
}
...
2) If not use HMVC that you can run other controller:
...
$this->load->library('../controllers/product');
$this->product->show($slug)
...
And you must change routes:
$route['(:any)'] = "page/show/$1";
Sorry for my english but I tried...
for pages:
Make the name of your controller the same as the name of your page.
for Category:
Make the name of the controller the same as the name of your category.
The sub categories are the methods in the category controller.
Product name be the identity of the product that you pass on the url through anchor.
I guess this should do the job.

Custom Error Pages for particular controllers in CodeIgniter

If we call a method abc() within a controller named Example. Suppose abc() is not present in Example controller.
In such cases i need to display a custom error message instead of
404 Page Not Found
The page you requested was not found.
for this particular controller only.
I know we can set custom error pages, but it applies to all controller.
I need to use it with one controller only.
For Eg:
class Example extends CI_Controller
{
function index()
{
echo "index page";
}
function xyz()
{
echo "xyz page";
}
}
if i call example/xyz it displays output as 'xyz page'
but if i call example/abc it show page not found error. (i need custom message for this controller only).
Thank You...
You can do something similar to below. If the method exists call it otherwise display your own error message.
function _remap( $method )
{
// $method contains the second segment of your URI
if(method_exists($this, $method ) )
{
$this->$method();
}
else
{
//your custom coding here
}
}

FURLS with codeigniter

I am developing a website with many pages whose content will be fetched from database. The whole website is done in codeigniter.
I do not want to have url's like www.mywebsite.com/pages/1 or something.
Rather I would like to have url name (FURL) asked by the administrator at the time of entering the contents of the page. Suppose the admin enters FURL as : study-programs/animation
then the path should be shown as www.mywebsity.com/study-programs/animation
Any suggestions?
1) Foreach page store the page name (could use uri_title())
2) In the routes (application/config/routes) add something like;
$route['page/(:any)'] = "pages/page_lookup";
3) In the pages controller;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function page_lookup($page) {
// do your database query getting page id where page name = $page
$result = $this->page_model->get_page_by_name($page);
// $result could hold the page html
// if so, then just do
$this->load->view($result);
}
}
For more details look at the examples in the Codeigniter manual.

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

Resources