Codeigniter admin controller in subfolder issue while pagination - codeigniter-2

I am new to codeigniter.i have create subfolder for admin controller like
Controller->admin->news.php
Now when i am access the news controller its working fine like
http://mysite.com/sacha/adminenter code here/news
But when i am trying edit delete or pagination like
http://mysite.com/sacha/admin/news/index/1
or
.../sacha/admin/news/1
Its showing 404 page not found error
Routes which i am using is
$route['admin/news'] = 'admin/news';
$route['admin/news/index'] = 'admin/news/index';
$route['admin/news/(:num)'] = 'admin/news/$1';
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';
I used (:any) also but none is working.
Thanks

remove your four line you specified above and just update your routes as this,.
$route['admin/news/index/(:num)'] = 'admin/news/index/$1';

$config['uri_segment'] = 4;
You have to include this config parameter in pagination.

Related

Codeigniter custom URL structure

I want Url in my codeigniter app like this,
in Home www.domain.com/ -> i route the $config defaut_controller to category
in page 2, www.domain.com/category/[category_name]/ -> in here (page 2) i create pagination.
in page 3, www.domain.com/category/[categori_name]/[sub_category1_name] ->in here i create pagination too.
and then, in page 4 i want www.domain.com/category/[categori_name]/[sub_category1_name]/[sub_category2_name].
In my database, I have table category, sub_category1, sub_category2. and string url in every table.
I was try using _remap($method, $params = array()) function, but i can't do it.
Can anybody give me approch to do this? or reference similar web structure.
You can use routes for this.
In application/config/config.php:
For page 1:
$route['default_controller'] = 'category';
For page 2:
$route["category/(:any)"] = "category/index/$1";
For page 3:
$route["category/(:any)/(:any)"] = "category/index/$1/$2";
For page 4:
$route["category/(:any)/(:any)/(:any)"] = "category/index/$1/$2/$3";
Side note:
If you still facing the issue than create seperate function in category controller for sub_category and change index to function name in controller.

how to make url as pretty url in codeigniter

I am new to ci. naybody knows how to minify the url.
for example : yourdmain.com/blog/view/blog-title
I need this url to be like this : yourdmain.com/blog-title
please explain how to do this
this can be many like blog, categories, pages, posts
please help..
Use route.php under your config folder
$route['blog-title] = 'blog/view/blog-title';
if you need dynamically loading based on a title
$route['(:any)/index.html'] = 'blog/view/$1';
// will route any url with /index.html to your controller
$route['(:any).html'] = 'blog/view/$1';
// will route any url with title.html to your controller then pass your title as your function variable
Why index.html or .html
This is my way i use to distinguish my other urls to my blog titles urls .. meaning only urls with extension index.html or .html will be routed to my blog/view path
You can have hyphens instead of underbars putting these lines in the file routes.php
$route['(.+)-(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4_$5";
$route['(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4";
$route['(.+)-(.+)-(.+)'] = "$1_$2_$3";
$route['(.+)-(.+)'] = "$1_$2";

how to make codeigniter routing to work with paging

I have the following routes for the news that works like a charm:
$word5 = urlencode('mygreek-keyword-here');
$route[$word5] = "news/displayAllNews";
and this page display all news with the desired keyword in the address bar. But...there is always a but...I have paging on this page, so when i click on next page link...page works but with old non-friendly seo url, that is:
news/displayAllNews/10/10
while I would like to have is: mygreek-keyword-here/10/10
code that i have in my controller for the paging is as follow:
$config['base_url'] = site_url('/news/displayAllNews/'.$limit.'/');
what should i do in routes.php and in my controller to get the desired result?
Regards, John
You could get the string of URI by $this->uri->uri_string() method, and set the pagination config as follows:
$config['base_url'] = site_url($this->uri->uri_string() .'/'. $limit.'/');
Note that the URI library is loaded automatically, it doesn't need to load it manually.
Update:
Change the route rule to:
$route["$word5(.*)"] = "news/displayAllNews$1";
If it doesn't work yet, remove the .'/'. $limit.'/' phrase and let the pagination to add the limit itself by:
$config['per_page'] = 10; // or $limit, default items per page.
$config['use_page_numbers'] = FALSE;

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

How to route everything to default controller in codeigniter?

I have a website http://example.com built using codeigniter 2. the default controller is
$route['default_controller'] = "domain";
If I try to access pageX, the link should be http://example.com/en/domain/view/pageX.
I want to allow the visitor of the website to access this page by typing
http://example.com/pageX
I have tried
$route['(:any)'] = "view/$1"; ==> it gives 404 Page Not Found
$route['(:any)'] = "domain/view/$1"; ==> it redirects to homepage with link shown as http://example.com/en/pageX
$route['(:any)'] = "en/domain/view/$1"; ==> it gives 404 Page Not Found
but non of them worked for me.
EDIT
by adding this:
$route['(:any)'] = 'domain/view/$1';
$route['en/blog'] = 'domain/view/blog';
example.com/blog will work fine
but I need it to be more general to cover all pages except admin page, something like this:
$route['(:any)'] = 'domain/view/$1';
$route['^(?!admin).*'] = 'domain/view/$o';
//The above routes will show the home page only for whatever i try!!
What is the route that i have to add to routes.php?
$route['default_controller'] is invoked if there is no URI present. Use $route['404_override'] for a full "catch all."
To get your routing pattern working, try this:
$route['[^/]*/(.*)'] = 'en/domain/view/$1';
I have managed my problem by doing this
$route['^[a-z]+$'] = 'domain/view/$1';
$route['([a-z]{2})/([a-z_]{1,50})'] = 'domain/view/$2';

Resources