Why is my main controller not working in codeigniter - php

I am brand new to codeigniter but I have been a PHP developer for years.
A client of mine has a exciting codeigniter site that was developed my another developer and I'm trying to set up a dev server to work on the site.
I can load the index.php in views if i go to this url
http://102.236.250.217/app/views/nycity/
But none of the header/footers or css are working
I feel like my controller "class Nycity extends MY_Controller" is not working what could be the cause of this?
This is my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>

Read codeigniter's user guide, learn directory structure, it's MVC. Find where your index.php file is and point domain there.
The rest is magic. Good luck!
P.s. MY_Controller should be located inside application/core according to the rules.

First, you have to check your route default controller, you should find routes.php in config folder, and this what you should write in:
$route['default_controller'] = 'your_default_controller';
Depending on your description, it should be like this
$route['default_controller'] = 'app';
And in your app controller should load the "nycity" view

Related

live server shows 404 error, but not on local, codeigniter 3

I have four identical pages that only differs in some identifiers but the logic is very much the same. All of them works on localhost. Three of those pages works okay on live server, but one of them returns a 404 for some reasons I can't understand.
I use XAMPP 3.2.2 and Sublime for developing it on local. I moved it to GoDaddy Deluxe Linux for live. My Codeigniter version is 3.1.10.
I already tried answers from most of the questions on StackOverflow:
All controller and model names' first letters are capitalized in the file name and inside the file.
All of the .htaccess codes on here: https://github.com/tasmanwebsolutions/htaccess_for_codeigniter
I have two .htaccess files, one of them is inside the application folder, and one is outside. I don't know if that's confusing the system? But I have another website on a different hosting that has the same .htaccess files and they're working okay. And anyway, if that's confusing the system, why does it only affect one of the four identical pages?
.htaccess outside application folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
.htaccess inside application folder:
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
All of the controllers of the four files has this up top:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//session_start(); //we need to call PHP's session object to access it through CI
class Question1 extends CI_Controller
{
and the rest is normal code stuff that are working on three of those four files.
In config. php I have:
$config['uri_protocol'] = 'REQUEST_URI';
and base_url is great.
Models and views are okay, as far as I can see, and the rest of the website is functioning alright with the included .htaccess. My routes are working okay, as well.
I'm so lost right now.
PS. I have a custom 404 page. When I input assessment.com/index.php/question1 it shows that custom 404 page. But when I open assessment.com/question1 it shows codeigniter's own 404 page (and not my custom one, for some reasons). I think that tells something, I just don't know what.
EDIT
Here's my routes.php:
$route['default_controller'] = 'home';
$route['404_override'] = 'Error404';
$route['translate_uri_dashes'] = TRUE;
I opted not to do
$route['question1'] = "Question1";
in routes because it doesn't change anything.
Check your APPLICATION ENVIRONMENT mode. You can find it on your project root_folder/index.php and at line no. 57.
define('ENVIRONMENT', 'production');
Change the ENVIRONMENT mode into the development mode, that is
define('ENVIRONMENT', 'development');
Then refresh your applications again. If you see CodeIgniter default error you can understand your CodeIgniter application are running ok and check out the error.If it doesn't show any error then your .httaccess has any error. I solved it my 404 error with this code, you can check this out.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^asset.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Answering this for the others who might come across the same problem.
Okay so user Don't Panic suggested I echo just "question x" in the problematic controller, and when I did that, the 404 error went away and it echoed the "question x." So definitely it's my controller that has the problem and not my routine or .htaccess files.
Now this controller is designed in such a way that it calls first an ID from the database before it shows anything. After I echoed "question x" I realized that the ID it is calling from my database has a value of zero. So, unsurprisingly, from the controller's point of view, the page doesn't exist! Hence the 404.
I've fixed it now by updating that table from my database and giving the ID a value it should already have had in the first place.

how to set homepage in Codeigniter?

I organized my controllers and views into subdirectories like this
frontend
admin
I created a Home.php in controllers/frontend
and a
home.php file in views/frontend
CI is installed in http://localhost/ci
when I access http://localhost/ci, I'd like the home view to load, but currently I can only access it via http://localhost:8080/ci/frontend/home
I'm guessing it may have something to do with my .htaccess file which is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also created the following constant:
define('BASE_URL', '/');
In the routes.php the default controller is set to:
$route['default_controller'] = 'frontend/home';
In the config.php, the base URL is set to:
$config['base_url'] = BASE_URL;
What am I doing wrong?
Thanks
Out of the Box CI 3.x.x doesn't support what you want to do. It's strictly controller/method...
But a little digging found this - Extending the Router Class to support subfolders
This allows you to override the behaviour of the router class and use sub folders.

.htaccess rewrite rule for self made MVC application

I have created a small web application in PHP and I am struggling with the .htaccess file
I have this file structure:
/Main Project
//App
// controllers, models, core, views
//Public
// css
What i did till now is to redirect from localhost directly to the /public folder like this:
RewriteEngine On
RewriteRule ^$ /public/ [L]
How I can do it to work the same as localhost/public/controller/action/params but just localhost/controller/action/param1/param2 ?
Thank you in advance for trying to help me!
So after trying it this days i found a solution for me and i can redirect to the public folder and also pass the controllers, methods and parameters. If somebody will have the same example, here is what works for me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This is basically the Laravel example :)

Laravel 4.2 : Route is conflict with subfolder name inside public folder

I'm got some problem with Route in Laravel 4.2. Here is details:
I have an example route:
Route::get('users', function(){return 'some-thing'});
and the route still work normally at http:://localhost:8000/users.
However, when I create a subfolder inside public folder, its name is "users".
--public
--users
That link didn't work and return the folder index. I know that is a big sercurity problem.
How to fix it? Can you please help me?
I wouldn't say a huge security problem, if there is sensitive data in that folder, then it shouldn't be in "public". You could deny directory listings (assuming Apache virtualhost or .htaccess):
Options -Indexes
As in Laravel's (and most other frameworks') .htaccess file, it only uses the "Router" if the file or directory doesn't exist. This is common as shown below:
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

CodeIgniter Noob - How does the index.php file work?

Here is my views directory:
- views/
- home.php
- contact.php
- assets/
- css/
- main.css
config.php: (changing this doesn't seem to do anything)
$config['index_page'] = 'index.php';
routes.php:
$route['default_controller'] = "welcome";
When I request www.example.com/index.php, the default page (i.e. home.php) loads and all of the assets work. However, when I request www.example.com/index.php/welcome or www.example.com/index.php/welcome/index, the page loads but the assets don't work. The same also happens if I try to load the page from a link from the home page.
I have no idea what index.php is for. I want to be able to just request www.example.com/welcome/index which will call the welcome.php controller and call the index method. application/index.php looks pretty important for everything to work, do I haven't deleted it, but I don't really want it. What can I do?
Thanks.
I think you're asking 3 different questions here.
Firstly index.php is the main entry point for your codeigniter app. It's very important as all of your routes will go through the index. The reason you can change it in the config is so you can rename it to something other than index.php if your setup requires it
Secondly, just guessing here but I think your assets are being loaded using a relative path; prefix all of your assets with base_url(); e.g
<?=base_url();?>assets/css/style.css
Thirdly, you'll need a htaccess file to hide the index.php (to give you www.example.com/welcome), or the equivalent if you're not using an apache server, which will look like the below (taken from http://www.codeigniter.com/user_guide/general/urls.html:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Enable mode Rewriting on your server LAMP/WAMP and add a .htaccess file with following code
RewriteEngine on
RewriteCond $1 !^(index.php|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
after doing above process you can use your urls without index.php i.e like www.example.com/welcome/index as well as your assets directory www.example.com/assets
Before all learn How MVC framework will work,
www.example.com/controller/function
If you call www.example.com , then default controller will load. You can change this in config.php
Inside the function you can call view ( your actual html viewable scripts ). follow the user guide from https://ellislab.com/codeigniter/user-guide
by default codeignitor url will be like www.example.com/index.php/controller/function
index.php can be removed using .htaccess

Categories