codeigniter redirect from a view - php

I am building a site entirely on codeigniter .I have set my default controller as cuff.
so whenever users type the domain name it takes the control to that controller.
class Cuff extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('index');
}
public function navigate()
{
echo "test";
exit;
}
}
In my index view I want an anchor to navigate to a function in my default controller .
so when I write
our collection , it says page not found .
I have even set the base url like this
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Cant seem to figure out the issue.

You missed the controller name
our collection

Set $config['base_url'] ='';
In your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|favicon|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Make sure your controller is in controller's folder (not any subfolder) and is properly named cuff.php
Try navigating to http://yoursite/cuff/navigate
If you still see errors try rewriting the last line in .htaccess to:
RewriteRule ^(.*)$ /index.php/$1 [L]
(without question mark).
Please tell if that helps!
EDIT: and your links better all be in this form:
Link

The problem is in your routing.
Edit your routes.php (located in the folder ../application/config/) and add the code below:
$route['navigate'] = "Cuff/navigate";
For more information regarding Codeingiter URI Routing:
http://codeigniter.com/user_guide/general/routing.html
Hope this helped you.

Simply you use
"<?php echo site_url();?>/cuff/navigate">our collection</a>

Related

unable to browse to codeigniter custom controller

I am having problems with codeigniter 3, I am using netbeans as my IDE and wampserver.
I was able to browser successfully to 'localhost/ci_test/' (ci_test is the name of the project/website') and it served me the default welcome.php controller,
however I tried a 2nd controller inside the CI controller folder called 'Test.php', with the following content:
<?php
class Test extends CI_Controller {
public function index() {
echo "This is default function.";
}
public function hello() {
echo "This is hello function.";
}
}
?>
then when i tried to browse to that controller: with the following url:
http://localhost/ci_test/index.php/test
it served me my welcome.php (which is not what I wanted),
then I tried this url:
http://localhost/ci_test/test.php
and I got a response:
Not Found
The requested URL /ci_test/test.php was not found on this server.
Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80
I don't understand what am I missing here...
Note: I did not change the .htacess files that were provided by codeigniter, and left them as is(maybe the problem there).
I only changed the following things in CI:
application/config/config.php:
$config['base_url'] = 'http://localhost/ci_test/';
$config['index_page'] = ''
$config['uri_protocol'] = 'PATH_INFO';
I hope someone can help here :)
EDIT:
I have read the first 4 answer's and it still doesn't work
EDIT2:
so after I decided to give up, I noticed that my apache server did not have ' mod_rewrite' ON, so i turned it on
tried again the same project and it still did not work, Then I decided to create a new project from scratch and with CI, I added new controller and everything works well now. this is lol...
Thank you very much for all those who tried to help me :)
You need to access that Test Controller directly then you need to add .htaccess file in the project root..
here is the .htaccess file for avoiding index.php in codeigniter
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
if you add this .htaccess file in the root your problem will resolved..
also don't need to add extention in your url (http://localhost/ci_test/test.php) , this is wrong
http://localhost/ci_test/test
this is enough to load your controller
welcome controller
<?php
class Welcome extends MY_Controller
{
public function index()
{
$this->load->view('welcome');
}
}
welcome view
Test Page
test controller
<?php
class Test extends MY_Controller
{
public function index()
{
$this->load->view('test');
}
}
test view
<h1>Test Page</h1>
The views must be named test.php and welcome.php and placed into the views folder.
Let us know how you get on.
Have a look at the routes.php file in the config folder
Change $route['default_controller'] = 'welcome';
to
$route['default_controller'] = 'test';
See if that works
If you get an error saying it can't find the page look at the address bar if it is like this
http://localhost/ci_test/test
try changing it to this
http://localhost/index.php/ci_test/test
Out of the box, codeigniter uses the url . com/controller/method.
You have created a Test controller called Test.php.
To access it you would use...
http://localhost/ci_test/test
You Do Not put a .php on the end.
This is a rushed quick answer... If you are using WAMPServer, check that it hasn't set MultiViews in either your httpd file or the vhosts file if you are using that.

unable to load your default controller on Codeigniter

This is the error that i am getting.
Please make sure the controller specified in your Routes.php file is valid.
Let me explain this.
In my application i have used HMVC. In my application folder there are two folders back-modules and front-modules.in my front-modules folder there is a folder called "home" and it has controllers,modules and views.
controllers->home.php
this is my home.php
class Home extends MX_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('form'));
}
public function index(){
$this->template->build('home');
}
}
and this is my routes.php
$route['default_controller'] = "home";
$route['404_override'] = '';
when i try to go to the site, it gives this error
unable to load your default controller on Codeigniter.Please make sure the controller specified in your Routes.php file is valid.
this system works fine on localhost.
Someone please help me in this matter.
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
your 'default_controller' should be:
$route['default_controller'] = "home/index";
read official CodeIgniter URI Routing Documentation
Kindly Check .htaccess file exist or not in root place of your website.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Some times codeigniter won't find the controller.
Go to config > routers.php
and see the value of default_controller
make a controller in controllers folder with the same value.
According to codegniter manual managing multi application
in your index.php file change
$application_folder = 'application';
to
$application_folder = 'application/home';
and make sure the controller file name starts with capital letter
controllers->Home.php
and keep Routes.php configurations
$route['default_controller'] = "home";

Codeigniter subdomain routing

I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.
The steps that I took to setup my new Blog controller involved:
Creating an A record pointing to my server's ip address.
Adding new rules to CodeIgniter's routes.php file.
Here is what I came up with:
switch ($_SERVER['HTTP_HOST']) {
case 'blog.notedu.mp':
$route['default_controller'] = "blog";
$route['latest'] = "blog/latest";
break;
default:
$route['default_controller'] = "main";
break;
}
This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.
Now here is the problem...
Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...
My .htaccess file looks like this (the default for removing index.php from the url):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And my Blog controller contains the following code:
class Blog extends CI_Controller {
public function _remap($method){
echo "_remap function called.\n";
echo "The method called was: ".$method;
}
public function index()
{
$this->load->helper('url');
$this->load->helper('../../global/helpers/base');
$this->load->view('blog');
}
public function latest(){
echo "latest working";
}
}
What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(
After 4 days of trial and error, I've finally fixed this issue!
Turns out it was a .htaccess problem and the following rules fixed it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks to everyone that read or answered this question.
Does blog.domain.co/blog/latest also show a 404?
maybe you could also take a look at the _remap() function for your default controller.
http://ellislab.com/codeigniter/user-guide/general/controllers.html#default
Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.
Straight from the user guide,
If your controller contains a function named _remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Hope this helps.
have a "AllowOverride All" in the configuration file of the subdomain in apache?
without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no
$route['latest'] = "index";
means that the URL http://blog.example.com/latest will look for an index() method in an index controller.
You want
$route['latest'] = "blog/latest";
Codeigniter user guide has a clear explanation about routes here

URI ROUTING in CodeIgnitor_2.1.3 throwing "Not Found" error

I have installed CodeIgniter_2.1.3 and running in
Windows 7
Wamp Server 2.1
PHP 5.3.5
Apache 2.2.17
In \applicationconfig\routes.php, I created
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
And in \application\controllers\welcome.php, I created a method
public function index()
{
$this->load->view('welcome_message');
}
public function getOneMethod()
{
echo "hi im in newMethod";
}
http://localhost/CodeIgniter_2.1.3/products/catlog
Now I expect on running this url above on browser to give me the page
hi im in newMethod
But instead i'm getting the error message.
Not Found
The requested URL /CodeIgniter_2.1.3/products/catlog was not found on
this server.
What should i do to make it work correctly?
Doo you visit codeigniter documentation website,You can get help from this url how to create static pages
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html
For the given url routes and controller.
you will need to use:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
The index.php part in the url can be removed using htaccess rewrite rule.
In the /codeigniter2.1.3/ folder create an .htaccess file and put the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CodeIgniter_2.1.3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /CodeIgniter_2.1.3/index.php [L]
</IfModule>
Note: This might not be the best way to do it.
EDIT:
Make sure the controller is exactly as bellow,
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function getOneMethod() {
echo "hi im in newMethod";
}
}
and the routes are as bellow:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
For the time being remove the htacces, and make it work with index.php in url.
ie:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
Note: since you are able to see the welcome message, there is no problem with your installation or server. there is probably some syntax error. so i recommend you copy paste the above code, as i have checked them and they are working.

CodeIgniter Views

Ok I have adopted a CodeIgniter Setup,
My route.php in config looks like this:
$route['default_controller'] = "development";
My development.php controller looks like this:
class Development extends Controller
{
function Development() {
parent::Controller();
}
public function Index() {
$this->load->view('index');
}
function show() {
$this->load->view('show');
}
}
When I go to the root folder, in my browser, it does load the index.php view, I want to make a link to show.php which is also in my Views dir. the URL I'm using is eg: my.server/test/codeigniter/ but when I go to my.server/test/codeigniter/show my show.php doesn't load. Am I doing this correctly?
I should mention I've tried public function show() also and it doesn't work, also I have no .htaccess file in the directory
Any advice would help!
Two rules are enough in your .htaccess file:
# check if the requested resource is not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# rewrite internally to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
Mod_rewrite and AllowOverride All assumed enabled.
Ken Struys comment on your question is correct. The default controller status you give a controller means that controller and the index function will load if you go to
my.server/test.
Other than that, you need to include index.php/controller_name/function_name to get to your controller functions.
Unless that is, you implement some fancy .htaccess url rewriting, which I can't help you with.
You can set it in routes.php as below:
$route['show']='development/show';

Categories