How to use redirect helper in codeigniter - codeigniter

I'm trying to redirect after the login.
After login it has to call the home controller's method.
For example if the url is test.com, I have two controllers: logincontroller and home. So the first url is:
test.com/logincontroller/login
After that, i want to redirect to
test.com/home/getfirstpage
So I used redirect function like this
redirect("/home/loadfirstpage", 'refresh');
But the problem is that the url get screwed up.
After redirect function it goes to this url:
test.com/logincontroller/test.com/home/getfirstpage
I want to fix to
test.com/home/getfirstpage.
How can i do this?
Edit: I'm assuming this is cuz of the .htaccess to delete index.php
This is my .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Check your config file and set the $config[base_url] correctly.
Load:
$this->load->helper('uri');
Then in controller:
redirect('controller/method', 'refresh');

The problem comes from the forward slash you put in the redirect
redirect("/home/loadfirstpage", 'refresh'); instead do this redirect("home/loadfirstpage", 'refresh');

Try this:
redirect("home/loadfirstpage", 'refresh');
And set your .htaccess file to:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
And make sure that you have set $config['index_page'] in config.php:
$config['index_page'] = '';

Related

Unable to route to static PHP page in CodeIgniter

I'm a newbie to CodeIgniter. In my 'views' folder, I've created 2 PHP pages - home.php and register.php. I've also created the header and footer PHP pages in a templates folder.
Here is my pages.php code which is the controller class:
<?php
class Pages extends CI_Controller
{
public function view($page)
{
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
and here is my routes.php code:
$route['register'] = "pages/view/register";
$route['default_controller'] = 'pages/view/home';
I have not yet written anything into the .htaccess file.
My project home directory is 'ED'
I am able to navigate to the home page with this URL: http://localhost/ED
However I'm unable to navigate to the registration page with these URLs:
localhost/ED/register
or
localhost/ED/register.php
Please help as to how can I achieve this.
You have to have the index.php by default because according to CodeIgniter URLs
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
So if you want to remove the index.php, you can follow this simple step:
You can easily remove this file by using a .htaccess file with some
simple rules. Here is an example of such a file, using the "negative"
method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP
request other than those for index.php, images, and robots.txt is
treated as a request for your index.php file.
Just put your .htaccess file(with the mod rewrite) in you main appplication folder and voila it's done.
What I did with my .htaccess was this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /name_of_your_codeigniter_folder/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>

Eleminating base_url() in every view for CodeIgniter

I am using CodeIgniter for my web app and i want to avoid on keep using base_url() function in all my urls in the views.
Now this code is what i want :
Click me to create user
instead of
Click me to create user
However, specifying "/user/create" will redirect me to below url which is incorrect
http://localhost/user/create
What i want is that "/user/create" url in href should automatically redirect me to
http://localhost/myapp/user/create
without specifying base_url()
Is this possible?
This is my current .htaccess file
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /myapp/
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Any help would greatly be appreciated.
Thanks
you can use form helper library
//Controller
function __construct()
{
parent:: __construct();
$this->load->helper('url');
$this->load->helper('form');
}
//View
<?php echo anchor('user/create','Click me to create user');?>
This will create similar result as
Click me to create user
They are way much better than using base_url() for links.
Try this:
href="user/create"
Just remove the first slash (/) before user and try.

Codeigniter redirect solution

Trying to get
www.example.com/home
to go directly to
www.example.com/
What I've tried:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]
What am I doing wrong?
but its website rotating always .
Redirect 301 /home http://www.example.com/
If what you are saying is that you want the index function in your "home" controller to be the default homepage, you can set that in routes.php under the route['default_controller'] parameter. That way you don't need to worry about the redirect at all. If you want to remove /home anytime it's typed in, you can use a mod-rewrite like you do to remove index.php from the uri. Check out the docs for more info http://ellislab.com/codeigniter/user-guide/general/urls.html

CodeIgniter form_open not hitting controller

Hi all I am new to CodeIgniter framework. I tried to develop a page which contains a form.
Here is the view
<?php echo form_open('Site/check');?>
User Name: <?php echo form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " ')); ?>
Add User:<?php echo form_submit('submit','Add User','class="btn btn-primary"');?>
<?php echo form_close();?>
This is the controller
<?php class Site extends CI_Controller{
function login() {
$data['records']="BHOOM!!!";
$this->load->view('login',$data);
}
function check() {
$this->load->view('showmessage');
}
}
?>
The showmessage is a PHP page with a hello in <h1> tags.
I already googled and searched for similar issues, but I didn't get any resolution
The base url is localhost/ci/index.php
Error
Unable to load the requested file: showmessage.php
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|js|css|img\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Here is a tutorial straight from EllisLab. Follow steps and you will get your form working.
The most common mistakes,
not loading form helper
htaccess. is set wrong, or paths are wrong (capital letters etc.).
config.php is not set properly
not passing data to view ($this->load->view('some_view', $data);
Hence you did not provide us error message if one exists there is no possible help from the community.
For more debug info please turn on codeigniters profiler ($this->output->enable_profiler(TRUE);), use PHPs native function var_dump($variable);.
Make sure error reporting is set to E_ALL in your index.php
edit
Make sure your /config/config.php
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";
make your .htaccess this file should be located in root of your project eg. /ci/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci/
# Reenable after getting ssl cert
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Removes access to the system folder by users
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# When your application folder isn't in the system folder This snippet prevents user access to the application folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Checks to see if the user is attempting to access a valid file, such as an image or css document, if this isn't true it sends the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
just use this type of code for your trial
view
<?php
echo form_open('controllet_name/method_name');
form_input(array('id'=>'Username' ,'name'=>'username' ,'class="input-medium " '));
form_close();
?>
controller
()
{
$name=$this->input->post('username');
echo $name ;
}
I think your RewriteRule is wrong.
Try:
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

How can i route to a controller and function in codeigniter?

I need to route for example to "www.url.com/controller/function" i put the code in 'config.php' in routes but, doesn't work. i need to load to views in the same controller, without put 'index.php' in the url. It is possible?
By default, the index.php file will be included in your URLs:
www.url.com/index.php/controller/function
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
Put the .htaccess file containing the above code to your app root folder.
here is the helping url to remove the index.php from url
User Guide remove index.php
you have to use .htaccess file to remove index.php from url
Hope it helps
Updated
Have a look in the application\config\config.php file, there is a variable named 'index_page'
It should look like this
$config['index_page'] = "index.php";
change it to
$config['index_page'] = "";
here is the .htaccess file you can use this..
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Here are some use full links.. you can follow these..
remove index.php stackoverflow
remove index.php codeigniter forum
Enable mod rewrite from your server
Hope these will help you

Resources