unable to call different class after index in codeigniter - codeigniter

I just started learning codeigniter and came up with some problem I could not sort.
In routing the default controller = 'home'
the base_url()=localhost/CodeIgniter_2.1.3/:
So when the site is loaded the site_url()=localhost/CodeIgniter_2.1.3/index.php
Home controller contains a link , to register controller.
So when this register controller is inside home object, and when the is linked to localhost/CodeIgniter_2.1.3/index.php/register it works fine.
But I want to make my Register controller a different object so that, I can go to register page like this localhost/CodeIgniter_2.1.3/register which I cannot get done. I tried messing with 'routes' but no luck there. Any Ideas?

create in your root directory a .htaccess file with following rules to use URI segments on routing properly :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
more at codeigniter documentation

Related

Custom route not working in codeigniter

I believe that I already did all the important thing to set up the custom route in codeigniter but I still don't know why I always get 404 error.
The current url I can access is: http://localhost:8080/project/api/profile_test/
And I want to rewrite it as: http://localhost:8080/project/api/users/
I've added this code in the route.php but still not working: $route[‘users’] = 'profile_test';
My .htaccess is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
I already set the AllowOverride All and remove the index.php in $config['index_page'] = '';. But still not working.
I’m almost 8 hours for this with this little thing and I need help to the one who knows it.
What am I missing? Any help is much appreciated.
In Codeigniter, a route consists of 3 parts:
the URL
the controller name
the method name
The docs explain it well.
So, if your project's root is http://localhost:8080/, for a URL like project/api/users/, to be handled by a controller (located in application/controllers/) called Profile_test and a method called index, you would need a route like:
$route['/project/api/users'] = 'profile_test/index';
Note that the controller file and class must be capitalised, as described in the docs, so for example controllers/Profile_test.php.
If your project's root is http://localhost:8080/project, you would change that to:
$route['/api/users'] = 'profile_test/index';
If your controller is located in a subdirectory like application/controllers/api/, you would change that to:
$route['/project/api/users'] = 'api/profile_test/index';
Also make sure you are using normal single quotes. Some of the code you include in your question includes 'smart' quotes: $route[‘users’] which will not work in PHP.

CodeIgniter Only Index Page is working

I recently started using codeIgniter and I am stuck in a problem.
I've two files in my view folder
index.php
profile.php
When I go to the url http://localhost/php/ci/index.php/ it shows my index.php page and everything is fine.
But when I go to http://localhost/php/ci/profile.php/ it says
The requested url was not found on server.
Why is this happening what's the problem ??
My Controller Files Names Are:
home_control.php
profile_control.php
home_control will interact with index.php and profile_control will interact with profile.php.
You have a fundamental flaw in your MVC understanding. The index.php file sitting in your document root (that is, your www directory) is actually responsible for getting CodeIgniter started on every request. That is the index.php that you are seeing in the URL bar.
You are confusing said index.php with the index.php that you fabricated inside of the views folder. Never will you be able to create a file inside of your views folder and be able to immediately access it using the URL bar. You must go through a controller.
If you access http://localhost/php/ci/index.php/profile_control and controllers/profile_control.php contains::
<?php
class Profile_control extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->view('profile')
}
}
You will be able to see the contents inside of views/profile.php
To reduce confusion, it is essential that you read this before going forward.
That is the wrong way to do it, read the manual about how to setup your .htaccess file and how routing works. You don't need to have multiple index.php-like files, the should only be one.
Here is the link from where you should start reading.
Like the other said. Everything goes through index.php. But you can actually hide it so that you don't have to type it in the url, which is not pretty btw.
Go to your applications/config/config.php and find something like the following:
/*
|--------------------------------------------------------------------------
| 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'] = "index.php";
remove the index.php. And in the .htaccess (which is next to your index.php) you need to add this If I'm not mistaken:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If everything works you can call your profile controller like http://localhost/php/ci/profile_control. By default it calls the index action so make sure you have an public function index in that controller. The url structure is always http://url.com/*controller*/*action*/*extra params here*
If you wish to call another action (function) inside your controller, for example public function profile(), you can call this url http://localhost/php/ci/profile_control/profile.
It is in that second action that you specify your other view file with $this->load->view('profile'). That will call the view/profile.php file
You can also pass values to that action like this -> http://localhost/php/ci/profile_control/profile/id/7.
in your profile action you need to grab those values as follow
public function profile($action, $value)
{
//$action = the word id and $value = the number 7
}
You can create another .htaccess file inside the project folder but outside application folder with the following code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also had the same issue where only my index page was working and It worked for me!

Code Igniter URL routing confusion

I have a slight issue. I have configured my default controller as :
c$route['default_controller'] = 'news';
so when I point my browser to http://localhost/mysite all the news get loaded as instructed by the news controller.
Now on this page, I have given links to "read article" to read the full article. For this I have to point my browser to http://localhost/index.php/news/view/news-slug. I want to remove index.php from the url. I had already tried re-routing using wildcard without any success. Can you tell me how to do that?
There is a documentation on removing the index.php from the URL if you are using Apache with it's mod_rewrite:
Removing the index.php file
Using the following rewrite code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also, there are many similar questions to yours, have a look at these questions from the search:
Search query on removing index.php in Codeigniter
You need to define your .htaccess file for this. Check this wiki: http://codeigniter.com/wiki/mod_rewrite

creating admin folder inside CodeIgniter App

I have the front end fully working in a codeIgniter application. Now, I have to create admin as well. So, How would I create the admin section creating a new directory. Without interrupting codeigniter directory structure.
localhost/myapp/admin
CodeIgniter already supports 1 subfolder level within the controllers folder. So within /applications/controllers/ you can just add /applications/controllers/admin/ and it will work fine.
you could omit it out via .htaccess so that the directory actually works like a directory rather than how its initially developed to work.
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
what this does is turn on apache modrewrite then tells apache any calls to index.php, robots.txt or yourdomain.com/admin/ (and any sub-folders/files within) treat as you would normally without codeigniter mucking up the works. Also this will remove the required index.php from the URL you will be able to link to your site like
mydomain.com/home/
instead of mydomain.com/index.php/home/
There's an explanation about that in CI User Guide: Managing your Applications

How to have an exception to URL routing in CodeIgniter

I have some legacy code that needs to be used in a new application. I would like to write the new application in CodeIgniter, but I still need to have some way of accessing the old code. what I would like to do is have an exception in the routing so that any url that has the format of example.com/old_stuff/* goes to an old_stuff folder, and acts as a regular, un-routed applications, while any other url such as example.com/new_stuff would route to a new_stuff controller, for example. So essentially what I want it to have URLs behave as they usually would in CodeIgniter, with the exception of any that start with one certain string.
What's the best way to accomplish this?
place codeigniter at your web root, and have your folder old_stuff in the web root also.
then use .htaccess with these rules (assuming you have mod_rewrite)
RewriteEngine on
RewriteCond $1 !^(index\.php|images|old_stuff|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
then, a uri beginning with old_stuff will just serve up the content bypassing codeigniter.

Resources