My class autoload function is not working in my mvc - php

MVC uploaded on cloud9, but when the auto-load class located in lib/init.php is not working. The .htaccess in the project folder or webroot folder files are not properly configured.
When I visit my project link https://my-mvc-hunteelar.c9users.io/ it gives this exception :
Failed to load class: Config' in /home/ubuntu/workspace/lib/init.php on line 21
I have 2 .htaccess files. The first .htaccess code in the main project folder is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
The second .htaccess file located in the webroot folder which is supposed to be the public folder of my MVC:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [PT,L]
</IfModule>
The init.php code which contains the __autoload function.
require_once (ROOT.DS.'config'.DS.'config.php');
function __autoload($class_name){
$lib_path = ROOT.DS.'lib'.DS.strtolower($class_name).'class.php';
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
$models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';
if(file_exists($lib_path)){
require ($lib_path);
}
elseif (file_exists($controllers_path)){
require_once ($controllers_path);
}
elseif (file_exists($models_path)){
require_once ($models_path);
}
else{
throw new Exception('Failed to load class : '.$class_name);
}
}
Here is my code uploaded on cloud9 https://ide.c9.io/hunteelar/my-mvc

I think you already solved this. But the problem is with the initialization of your code.
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'class.php';
should actually be:
$controllers_path = ROOT.DS.'controllers'.DS.str_replace('controller', '', strtolower($class_name)).'.controller.php';
Also
$models_path = ROOT.DS.'models'.DS.strtolower($class_name).'class.php';
Should be
$model_path = ROOT.DS.'models'.DS.strtolower($class_name).'.php';
In case it may help someone else.

Related

Rewriting .htaccess to remove index.php with alias url on codeigniter

I have an issue with .htaccess codeigniter on our website (www.jp.com => first project)
I just recently built a new project and i set and accessed by an aliased link. (www.jp.com/pt => second project).
A path looks like this :
first project : /var/www/jp
second project : /var/www/pt
The problem, if i access this url : "www.jp.com/pt" it's works,
but.. if i access others controller, e.g: "www.jp.com/pt/news" where i'm not set a controller as a default controller, the page shown 404 not found.
And then,i try using index.php like this, "www.jp.com/pt/index.php/news" It's works..
Can anyone help me to fixing this ? i'll can access my alias link without index.php
I using CI 2 & PHP 5.3
Here is my code (mixed from many references :D )
Current .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^/(.*)$ http://www.jp.com/$1 [NC,L,R=301]
My Config.php :
$root = "http://".$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$config['index_page'] = '';
Thank you...
The .htaccess RewriteBase in the codeigniter second project directory should say /pt/
RewriteBase /pt
Otherwise the rewrite to index.php ends up going to first project router.

CodeIgniter 3 Routing Issue

I used CI's routes and have the following entry in config/routes.php
if($subdomain == 'debug')
{
$route['debug/(:any)'] = "admin/debug/$1";
}
elseif($subdomain == 'dev')
{
$route['models/(:any)'] = "v2/dev/models/$1";
}
If I were to browse to
dev.mydomain.com/models/test/
everything works fine and I get the correct output.
However, I were to browse to
dev.mydomain.com/models/test/trip_id/9091
I will get a 404 Error page.
Only way to make it to work is to use the following route entry.
$route['models/(:any)/(:any)/(:any)'] = "v2/dev/models/$1/$2/$3";
What really confuses me further is that it works if I were to browse to
debug.mydomain.com/debug/test/trip_id/9091
What am I missing here as the route entries looks similar but will only work on the first subdomain (debug.mydomain.com) but not on the latter (dev.mydomain.com)
This is the content of my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help is greatly appreciated.
$route['models/(:any)'] = "v2/dev/models/$1";
Check the route path if you are calling the controller from the folder
it should be yourfolder/controller/method_name/$1

Laravel .htaccess rewrite URL for removing public

I have a Laravel project
My domain is domain.com.tw
and the URL will be domain.com.tw/public,
and the page could be showed.
I want to remove the public in URL,
so I write RewriteRule in .htaccess with:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
but it did not work for me
I had look at these also, but still failed
Apache Mod Rewrite For Laravel
Laravel 4.2 rewrite URL - remove public
could someone tell me the solution? thanks!!
Create a folder on root with named “whatever”(you can give any name).
Move all files except public folder in the “whatever” folder.
Move all the files of public to root directory and remove blank public folder.
so directory structure will be:
whatever– which have all root files except public folder
all files of public folder.
Now time to change some paths
in paths.php change the following code
'public' => __DIR__.'/../public', into 'public' => __DIR__.'/../../',
open index.php (on root) and find below code
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
change this to
require __DIR__.'/local/bootstrap/autoload.php';
$app = require_once __DIR__.'/local/bootstrap/start.php';
Hope this helps or at least give you some idea

Codeigniter showing 404 Page Not Found on Hostgator

I'm currently testing my Codeigniter Project on my own Hostgator account. The project works well on my WAMP server but it shows a 404 Page Not Found error when online on Hostgator. I've tried playing around with the file permissions on FTP to playing around with the .htaccess file. The current site can be viewed here: www.test.jeffreyteruel.com.
Here's a look at my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My current file structure:
-application
-assets (css/js/img files)
-cgi-bin(from the server)
-system
-uploads
-.htaccess
-index.php
Here's my current config.php info:
$config['base_url'] = 'http://test.jeffreyteruel.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
The default controller on the route.php file is: $route['default_controller'] = 'main';
Main Controller (main.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
//insert model here
$this->load->model('main_model');
date_default_timezone_set('Asia/Manila');
}
public function index()
{
$content['main_content'] = 'home/home';
$this->load->view('main',$content);
}
...
?>
Any help to get the site showing properly would be appreciated.
I have CI running on a subdomain on Hostgator and I recall a slight struggle getting it working but as I'm old I've got a memory like a.... Hmmm can't remember!
My Simplified .htaccess is
RewriteEngine On
RewriteBase /
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Addition:
In your config.php
$config['base_url'] = 'http://test.jeffreyteruel.com';
Add the trailing /
$config['base_url'] = 'http://test.jeffreyteruel.com/';
UPDATE:
With Codeigniter 3 All Class names need to be Capitialized - First letter Uppercased ( ucfirst ).
Refer to : codeigniter.com/userguide3/general/styleguide.html#file-naming

Apache + PHP without ".php" File Extension but Including Path Info

I'm trying to figure out how to modify the .htaccess file so I can do two things:
Not have to include the .php extension on my PHP files (e.g., a request to my.domain.com/page maps to my.domain.com/page.php).
Do #1 while also including additional path info (e.g., a request to my.domain.com/page/path/stuff/here maps to my.domain.com/page.php/path/stuff/here).
I've found out how to do #1 by adding the following to the .htaccess file:
# Allow PHP files without ".php" extension.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /$1.php [L,QSA]
However, now I'd like to modify the RewriteRule so it works for #2.
OK, after searching for MultiViews, I found several articles warning against them (eh, to each his own), but that also led me to an answer that uses 2 rules instead of just 1:
RewriteRule ^([^\.]+)$ /$1.php [L]
RewriteRule ^([^\./]+)/(.*) /$1.php/$2 [L]
The first rule catches case #1 above, and the second rule catches case #2 above. Voila!
You could just try to use Multiviews, which is made to do exactly this:
Options +Multiviews
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([^\.]+)$ $1.php [NC,L] #Remove the .php
Not sure what you want with the pathing stuff though.
Edit based off your comment, I've used something like this with php/angular. It's probably not "correct" or the best way to do it, but it worked for me.
Htaccess
RewriteEngine on
# Allow the API to function as a Front Controller
RewriteRule ^api/(.*)$ api/index.php?rt=$1 [L,QSA,NC]
# Allow Angular to have Pretty URL's
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
api/index.php
// Pull the routing path
$router = explode('/', $_GET['rt']);
$version = $router[0];
$controller = $router[1];
$action = $router[2];
// Check for the file
if(file_exists($version . '/controllers/' . $controller .'.class.php')) {
include $version . '/controllers/' . $controller .'.class.php';
} else {
return false;
}
// Initialize and execute
$method = new $controller($action);
print $method->$action();
This lets me do something like: api/v1/users/login in the url, then will find the users.class.php file in the V1 folder, and run the function login.

Categories