How can I remove method name from URL in codeigniter? - php

I am using the multilingual feature in my Codeigniter website.
current URL looks like
http://example.com/hi/news/view/news-title
I want this like
http://example.com/hi/news/news-title
current routes.php is looking like
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
//Routes for multilingual url
$route['^hi/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
// '/en' -> use default controller
$route['^hi$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
.htaccess file looks like
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
I tried these but it not works and always give me 404 error
$route['^(en|hi)/news/(:any)'] = "news/view/$1";
$route['hi/news/(:any)'] = 'news/view/$1';

You have not specified the name of the controller or the controller called "News" is not exists.
Specify the name of the controller that is having the "news" method e.g.
$route['(en|hi)/news/(:any)'] = "home/news/view/$1";
OR
Create a "News" controller and create a "view" method e.g.
class News extends CI_Controller {
public function view()
{
}
}

Related

Codeigniter invalid link opens as a valid link

I tried building a news section page on my codeigniter project.
i have my class 'admin' and a function 'articles', normally the valid url is localhost/admin/articles
but when i visit localhost/admin/articles/anything it still opens the articles page.
Below is the code for articles
<?php
class Admin extends CI_Controller {
public function articles() {
$data['volume'] = $this->user_model->get_volume();
$data['issue'] = $this->user_model->get_issue();
$data['article'] = $this->user_model->get_articles();
$data['title'] = "Admin Panel - Articles";
$this->parser->parse('admin/head',$data);
$this->parser->parse('admin/articles',$data);
}
}
my route.php
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
.htacess
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
The normal url is localhost/admin/articles, but adding anything after articles, e.g localhost/admin/articles/hello/world , still opens the articles page.

Codeignite hello world code in docs doent work

I just started using CodeIgniter and I'm stuck at the hello world code in the docs. Each time I type in the name of the blog controller in the url bar and hit enter, I get this error:
404 Error Page Not Found!
localhost/php/CodeIgniter/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
route.php
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
config.php
$config['base_url'] = 'http://localhost/php/CodeIgniter/';
$config['index_page'] = 'index.php';
controllers/Blog.php
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
How Can I Resolve This?
You have to set a URL string on [route.php] corresponding controller class/method.
$route['default_controller'] = 'blog/index';
$route['blog/(:any)'] = 'blog/show/$1'
You can leave the URL "http://localhost/" referencing "/" as home by default_controller on route.php
$config['base_url'] = 'http://localhost';
Please browse more about .htaccess and friendly URL. If you get 404 error maybe your URL is not friendly.
Friendly:
http://localhost/blog/lorem-ipsum
Not Friendly:
http://localhost/index.php/blog/lorem-ipsum
Documentation: https://www.codeigniter.com/userguide3/general/routing.html

Codeigniter 3 - how to callback to home

if i´m on a category/index/$1 Site of my Page and want to call back to domain.com/home, it doesn't work for me.
Thats my routes
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['admin'] = 'admin/login';
$route['category/(:any)'] = 'category/index/$1';
$route['category/(:any)/(:num)'] = 'category/index/$1/$2';
$route['page/(:any)'] = 'page/index/$1';
If i call domain.com/about and click on my navigation "home", its working. but if i make domain.com/aboutdetails/index/1 he make on home click this one:
domain.com/aboutdetails/index/home
htaccess
#RewriteEngine on
#RewriteCond $1 !^(index\.php|images|robots\.txt|style|cdn|check\.php)
RewriteEngine on
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I think, you can use :
Home
If you want go to home with your configuration router using domain.com.
If you want use domain.com/home you add next line route:
$route['home'] = 'home/index';
Considerer use initial en and limiter( regex ), example:
$route['^home$'] = 'home/index';
Good, To print link don't use base_url(), it is use to load assets or other an return project root, Use site_url().
In the application/config.php set empty to $config['index_page']:
//$config['index_page'] = 'index.php';
$config['index_page'] = '';
And then create link with site_url():
site_url( 'home' );
If you want create link with more params you maybe pass array.
site_url( array( 'home', $param2 ) );

CodeIgniter setting up routing properly

I'm trying to setting up my codeigniter routing to make an authentication form for an admin panel, but the routing is not working. I'm a beginner with CodeIgniter and I think I'm missing something.
In my server I put the CodeIgniter fiels inside folder in my root directory, so it can be accessed like this:
/localhost/ci
In my config.php I set my base url and remove the index page to remove index.php:
$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
Also I have edited the .htaccess to remove the index.php like CodeIgniter documentation says, so it looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my routes config file looks like this. I set my default page and a route to my Auth controller:
$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";
This is my Controller:
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
// load libraries
}
function index() {
if (! $this->ion_auth->logged_in()) {
// redirect them to the dashboard page
redirect(base_url('auth/login'), 'refresh');
} else {
// show the dashboard page
redirect(base_url('dashboard'), 'refresh');
}
}
function login() {
$this->load->view('login');
}
When I enter in the web-browser http://localhost/ci/ it redirects me to http://localhost/ci/auth/login, but then a 404 Not Found error ir raised. What am I missing? I'm a bit confused at this point, thanks.
Try changing your .htaccess from:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

Codeigniter Routing Issues on AWS EC2

I have been searching the stackoverflow all day for an answer to this and I just can't seem to find one.
I have my Codeigniter app on an AWS EC2 server.
Here is my routes.php:
$route['default_controller'] = 'pages/home';
$route['(:any)'] = 'pages/$1';
EDIT Here is my current routes.php:
$route['default_controller'] = 'pages/home';
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['home'] = 'pages/home';
Here is my Pages.php:
class Pages extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$this->view('home');
}
public function about(){
$this->view('about');
}
public function contact(){
$this->view('contact');
}
public function view($page)
{
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
Here is my .htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
My config.php:
$config['index_page'] = '';
PROBLEM
If I go to (my-url), it uses my default_controller.
If I go to (my-url)/home or /about or /contact, I get a 404 error.
If I go to (my-url)/index.php/home or /about or /contact, I get the default_controller.
I have double checked that mod_rewrite is installed and enabled. I can't seem to figure out why I'm not getting anything. I followed the tutorial and read a bunch of stackoverflow questions and answers to try to solve it, but to no avail.
Please help.
You can not use routes as like that:
$route['(:any)'] = 'pages/$1';
It will not work with all functions as you need.
Just Suggestion:
Use your routes as:
$route['about'] = 'pages/about';
$route['contact'] = 'pages/contact';
$route['view/(:any)'] = 'pages/view/$1';
......
You need different routes for different URL if you use (:any) as first param it will not work properly for all functions.
Try this
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
Note when using APPPATH constant, the path ends with '/' already
try change
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
to
if (!file_exists(APPPATH.'views/pages/'.$page.'.php'))

Categories