Codeigniter invalid link opens as a valid link - php

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.

Related

How can I remove method name from URL in codeigniter?

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()
{
}
}

Duplicate content issue due to Codeigniter controllers

I am assisting with a Codeigniter website and don't have a background with it. It has been noted that there is duplicate page content. When I dug in, they are not actual pages, but controllers pointing to the same views. I attempted to set up 301 redirects in an .htaccess file in the root of the website, but it is not taking. In the application/config/routes.php, the following is noted:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
For example, there is a page http://mywebsite.com/weddings, and then http://mywebsite.com/Home/weddings was another controller that pointed to the same view. I attempted to add a 301 redirect from Home/weddings after removing the additional controller pointing to it, but now Home/weddings just redirects to the home page, as it is set up with the 404 override. I hope I have provided enough information for some assistance.
.htaccess file:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1
!^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^Home/weddings/$ /weddings
Home.php is as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Admin_model');
$this->load->model('Home_model');
$this->load->helper('captcha');
$this->load->library('recaptcha');
}
public function weddings()
{
$this->load->view('Template/Home/front_header');
$this->load->view('Home/weddings');
$this->load->view('Template/Home/front_footer');
}
Do not touch .htaccess. You have two options.
Create a controller Weddings.php and in your index.php return a
view for weddings page.
The other option is to use Home.php controller and then add weddings method, the method should return the weddings page view. Your route should look like below:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
$route['/weddings'] = 'home/weddings';
Hope this will help. Thank you
Edit
In your Weddings.php controller constructor check if there exist a "Home" and return 404 error. See below
function __construct()
{
parent::Controller();
if($this->uri->uri_segment(1) == 'Home') {
show_404();
}
}

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

calling function from view in codeigniter not working

I am calling controller function from view with base url it's going to that function but showing 404 Page Not Found.
.htaccess I am using is this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
config.php
$config['index_page'] = '';
autoload is
$autoload['helper'] = array('form', 'url', 'security');
default controller in routes.php is
$route['default_controller'] = 'prog_bar';
base_url is
$config['base_url'] = 'http://localhost:8080/jsLearning/prog_bar/';
view
a href="<?php echo base_url().'file_func'; ?>">abc</a>
controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class prog_bar extends CI_Controller {
public function index()
{
$this->load->view('prog_bar_view');
}
public function file_func(){
echo "form";
}
}
what else I am missing?
Class name should be started with capital letter,
Change this
class prog_bar extends CI_Controller {
to
class Prog_bar extends CI_Controller {
Your base_url is wrong. It should be
$config['base_url'] = 'http://localhost:8080/jsLearning/';
and now in HTML use site_url() instead base_url() like
abc
base_url() should be used only for resources link like image file, css file etc
Update your .htaccess by following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I think you should be great with settings below:
In Config.php:
$config['base_url'] = 'http://localhost:8080/jsLearning/';
In Your View:
abc
In Your Controller:
class Prog_bar extends CI_Controller {
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have still error, check to see what your base_url link to..Create a link
Base Url
to see in your browser where it links.
You do not use the base url for links within the site. Use the controller name and the methods name
<?php echo anchor("Pages/entry", "Post Entry"); ?>
What is form? It is not defined. Is it a page? if so, you have no view address for it. If its a snippet, you still need an address
$this->load->view('prog_bar_view')
a function will not simply show a view or snippet without being told where it is. If you are trying to include a form in a page, then just use nested views and in your main(html) view load the view you are trying to show with $this->load->view()

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]

Categories