loading controllers in codeigniter - php

For register page I have 'Register' controller
For login page, I have 'login' controller
I loaded url, form helpers
at the end of I have given login link as follows
Login
But it's showing the error.
Object not found!
The requested URL was not found on this server.

Change your htaccess file to this. I think it will work.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url() will output the above string.

First Check Your base_url From application/Config/config.php file
Then Set Your base_url()
`$config['base_url'] = 'http://localhost/[your-project-name]';`
if your project have on root folder then set
`$config['base_url'] = 'http://localhost/';`
Note: In Login Controller May be You find index file. If not then must be set your function name. Like this:
Login/[your-function-name]

Login

Have you removed index.php from url using .htaccess file?
If yes the you can use <?php echo base_url('login'); ?>.
Or else you should use <?php echo site_url('login'); ?>.

$config['base_url'] = 'http://localhost/';
assuming your index.php is in htdocs directly and not some subfolder.
otherwise it should be $config['base_url'] = 'http://localhost/sub_folder/';
(trailing slash required)

Related

CodeIgniter base_url leads to page not found

I'm using CodeIgniter, and I'm new at it, as I'm in web development.
I'm having a problem with the base_url from CI: it leads to 404 Page not found.
Here is the referrence for the page:
<li>Cadastrar</li>
The controller's function:
function novo_usual(){
$this->load->view('html-header');
$this->load->view('html-footer');
$this->load->view('cadastro_usual');
}
My controller's name file starts in capital, like Usual.php, and it extends CI_Controller.
My altoloado.php:
$autoload['helper'] = array('url','html','form','funcoes');
My routes.php:
$route['default_controller'] = 'Home';
Finally the .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
See \application\config\config.php
$config['base_url'] = '';
write to this your site url.
for example
$config['base_url'] = 'http://test.com/';
and now if you use base_url('Usual/novo_usual') this is equal http://test.com/Usual/novo_usual
If you don't set site url then first of set its
//Site URL Path : application\config\config.php
$config['base_url'] = 'http://localhost/site_name';
Then add this :
<?php
echo site_url('Usual/novo_usual');
//OR
echo base_url('Usual/novo_usual'); ?>
At first you need to set your folder path in
\application\config\config.php
$config['base_url'] = '';
for example
$config['base_url'] = 'http://localhost/folder/';
I've managed to solve it.
Turns out that my project's name starts with 'G', and in my url I was calling it with 'g'.
Thanks guys.

Codeigniter css error loading images

I've been messing around because when i look at the page it says that he cant load up the image but there are no errors in debugger so its probably a framework error or htacess. This happens after i create my view news witch i accept a param in url (the 4th).
my htacess
ReWriteEngine On
RewriteCond $1 !^(index\.php|resources|assets|robots\.txt)
ReWriteCond %{REQUEST_FILENAME} !-f
ReWriteCond %{REQUEST_FILENAME} !-d
ReWriteRule ^(.*)$ index.php/$1 [L]
Seems like its not getting the full URL.. Give the total image URL along with the domain name like
http://www.yourdomain.com/assets/images/uploads/uploads/b.pn‌​g
Try setting your base url example on config.php
$config['base_url'] = 'http://loaclhost/youyproject/';
Or If have domain some thing like
$config['base_url'] = 'http://www.example.com/';
Then autoload url helper config/autoload.php
$autoload['helper'] = array('url');
And then try using like
<img src="<?php echo base_url('assets/images/uploads/uploads/b.pn‌​g');?>" />
Make sure assets folder out side of application
application
assets
system
index.php

Redirect the URL to another, in codeigniter

I have a webpage which was built with core php, now its been updated to codeigniter mvc. Problem which i am facing is, users are trying to enter the old URL path
example: www.website_name/old_path
now since the mvc layout, it wont work and will get 404 error. i want to redirect the clients who enters www.website_name/old_path for the new one
www.website_name/index.php/new_controller/new_view.
Anyone please help me to resolve this issue.
Thank You!
first of all remove index.php config file.
Open config.php and do following replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
Then create HTACCESS file in application/ folder
having this code....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now index.php is removed from your URl....
STEP 2) open application/config/routes.php file
Now here define your desired web URL
$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";
So,Your URL Will be ..
www.website_name/new_controller(or xyz)/
Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL
http://www.codeigniter.com/user_guide/general/urls.html
As Sumit mentioned, you should use URL Routing for this.
In the routes file you can create a list of all the redirects that you want like so:
$route['old_path'] = "new_controller/new_view";
You can use 301 redirect for this type of issue.
Put this code in .htaccess
RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]
or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html
// with 301 redirect
redirect('/article/13', 'location', 301);

Redirect home URL to main domain

I want to redirect home controller to my domain, it should look like this
http://sample.com/home to http://sample.com
http://sample.com/home/index to http://sample.com
http://localhost:8888/sample/home to http://localhost:8888/sample/
http://localhost:8888/sample/home/index to http://localhost:8888/sample/
is it possible? And how to do it?
When you need to redirect to home page redirect('/');
URL Helper
On your config base_url() make sure is set
$config['base_url'] = 'http://sample.com/';
You may need to remove index.php and have a suitable htaccess file in main directory.
$config['index_page'] = '';
Htaccess example for main directory
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Your home controller route should the be set on default route application -> config -> routes.php
Codeigniter URI Routing User Guide Page
$route['default_controller'] = 'home';
Then autoload url helper
$autoload['helper'] = array('url');
The if you need to redirect to home controller make sure file name Home.php
and use redirect('/');
Note: Codeigniter 3 is case sensitive make sure first letter all ways upper case on file name and class name

Redirecting a Url in codeIgniter to a root folder

I have a folder structure like this in Code igniter : -
- www
- CI
- application
- config
- config.php
- adminfolder
- system
- css
- jss
And now i want to redirect the Url when the user goes to the controller : formhandler and action: login of url, http://localhost/CI/formhandler/login to this : http://localhost/CI/adminfolder How to do this ? Thanks in advance .
This can be done by using URL Helper of CodeIgniter.
This helper library should br loaded using the following code:
$this->load->helper('url');
To redirect you just need to call function as follow. Put this code inside your login method.
redirect('http://localhost/CI/adminfolder');
Reference code: http://dwij.co.in/redirect-to-url-in-codeigniter
assuming your base url is:
$config['base_url'] = "http://localhost/CI/";
it's simply to do:
redirect(site_url('adminfolder'),'',301/*here your redirect http code*/);
and be sure the folder is accessible via browser
I had same issue and fix it by updating my .htaccess file with the following code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found it from https://codeigniter.com/user_guide/general/urls.html

Categories