I am creating a custom MVC framework in PHP based on the tutorial by Anant Garg:
CustomMVC
-app
--controllers
---Controller.php
---HomeController.php
--models
---Home.php
--views
-config
--app.php
-lib
--bootstrap.php
--shared.php
--database.class.php
-public
--.htaccess
--index.php
-tmp
-.htaccess
My HomeController cannot extend the Controller and I get the following:
Fatal error: Class 'app\controllers\Controller' not found in
/Applications/MAMP/htdocs/CustomMVC/app/controllers/homecontroller.php
on line 7
Both of them are under the same namespace and they are located under the same directory. I don't know why HomeController cannot extend the base Controller. Any advice on that?
namespace app\controllers;
Controller.php
namespace app\controllers;
use lib\Template;
class Controller {
protected $_model;
protected $_controller;
protected $_action;
protected $_template;
public function __construct($model, $controller, $action) {
$this->_controller = $controller;
$this->_action = $action;
$this->_model = $model;
$this->_model =& new $model;
$this->_template =& new Template($controller,$action);
}
function set($name,$value) {
$this->_template->set($name,$value);
}
function __destruct() {
$this->_template->render();
}
}
HomeController.php
<?php
namespace app\controllers;
use app\controllers;
class HomeController extends Controller {
function getHome($id = null,$name = null) {
}
function getAbout() {
}
function postContact() {
}
}
This is the autoload function in the lib**shared.php**
function __autoload($className) {
if ( file_exists(ROOT . DS . 'lib' . DS . strtolower($className) . '.php') )
{
require_once(ROOT . DS . 'lib' . DS . strtolower($className) . '.php');
}
else if ( file_exists(ROOT . DS . 'lib' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'lib' . DS . strtolower($className) . '.class.php');
}
else if ( file_exists(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.php'))
{
require_once(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.php');
}
else if( file_exists(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'app' . DS . 'controllers' . DS . strtolower($className) . '.class.php');
}
else if (file_exists(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.php'))
{
require_once(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.php');
}
else if (file_exists(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.class.php'))
{
require_once(ROOT . DS . 'app' . DS . 'models' . DS . strtolower($className) . '.class.php');
}
else
{
/* Error Generation Code Here */
}
}
Related
If I delete file like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
$result = File::delete($destinationPath . DIRECTORY_SEPARATOR . $filename);
It works
But if I delete folder like this :
$destinationPath = public_path() . DIRECTORY_SEPARATOR . 'img'. DIRECTORY_SEPARATOR . 'products' . DIRECTORY_SEPARATOR . $id;
File::delete($destinationPath);
It does not work
Why delete folder does not work?
How can I solve this problem?
You are attempting to use the wrong method. You need to use deleteDirectory and not delete:
$destinationPath = public_path('img/products/'. $id);
File::deleteDirectory($destinationPath);
If you want to delete folder with files than use:-
use Illuminate\Support\Facades\Storage;
Storage::deleteDirectory($public_path);
Directory structures is messed up while unziping on Linux while zip file created in windows using php ZIPArchive
Zip creation on Windows ---> Unzip on Windows --> No isuses
Zip creation on Linux ---> Unzip on Linux --> No isuses
Zip creation on Windows ---> Unzip on Linux ---> Files are not unziping to right directories
Structure of folder to be zipped
b2c
--> data
d.xml
---> metatada
m.xml
Create a zip using code below on windows platform. Now unzip on Linux . Here is the structure, instead of putting d.xml under b2c\data, it is creating a file data\d.xml.
b2c
data\d.xml
metadata\m.xml
Source code
<?php
define('DS', DIRECTORY_SEPARATOR);
/*
$folder_to_zip = 'c:'. DS .'temp'. DS . 'b2c';
$zip_file = 'c:' . DS . 'temp' .DS . 'b2c'. DS . 'b2c.zip';
create_zip($folder_to_zip, $zip_file);
$folder_to_unzip = 'c:' . DS . 'temp1';
$src_zip_file = 'c:'. DS . 'temp' . DS . 'b2c' . DS . 'b2c.zip';
unzip_file($folder_to_unzip,$src_zip_file);
*/
$folder_to_zip = DS. 'home' . DS . 'bitnami' . DS . 'temp' . DS . 'b2c';
$zip_file = DS. 'home' . DS . 'bitnami' . DS . 'temp' . DS . 'b2c'. DS . 'b2c.zip';
create_zip($folder_to_zip, $zip_file);
$folder_to_unzip = DS. 'home' . DS . 'bitnami' . DS . 'temp1' . DS . 'b2c';
$src_zip_file = DS. 'home' . DS . 'bitnami' . DS . 'temp' . DS . 'b2c'. DS . 'b2c.zip';
unzip_file($folder_to_unzip,$src_zip_file);
function create_zip($folder_to_zip, $zip_file) {
$rootPath = realpath($folder_to_zip);
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator /** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
}
function unzip_file($folder_to_unzip,$src_zip_file) {
$zip = new ZipArchive;
$res = $zip->open($src_zip_file);
if ($res === TRUE) {
$zip->extractTo($folder_to_unzip);
$zip->close();
return TRUE ;
} else {
return FALSE ;
}
}
Following CakePHP code is in my index.php file. When it runs on the server, it shows
internal server error 500
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT')) {
define('ROOT', dirname(dirname(dirname(__FILE__))));
}
if (!defined('APP_DIR')) {
define('APP_DIR', basename(dirname(dirname(__FILE__))));
}
$vendorPath = ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib';
$dispatcher = 'Cake' . DS . 'Console' . DS . 'ShellDispatcher.php';
if (!defined('CAKE_CORE_INCLUDE_PATH') && file_exists($vendorPath . DS . $dispatcher)) {
define('CAKE_CORE_INCLUDE_PATH', $vendorPath);
}
if (!defined('WEBROOT_DIR')) {
define('WEBROOT_DIR', basename(dirname(__FILE__)));
}
if (!defined('WWW_ROOT')) {
define('WWW_ROOT', dirname(__FILE__) . DS);
}
if (php_sapi_name() === 'cli-server') {
if ($_SERVER['REQUEST_URI'] !== '/' && file_exists(WWW_ROOT . $_SERVER['PHP_SELF'])) {
return false;
}
$_SERVER['PHP_SELF'] = '/' . basename(__FILE__);
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
if (!include 'Cake' . DS . 'bootstrap.php') {
$failed = true;
}
} else {
if (!include CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php') {
$failed = true;
}
}
if (!empty($failed)) {
trigger_error("CakePHP core could not be found. Check the value of CAKE_CORE_INCLUDE_PATH in APP/webroot/index.php. It should point to the directory containing your " . DS . "cake core directory and your " . DS . "vendors root directory.", E_USER_ERROR);
}
App::uses('Dispatcher', 'Routing');
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(
new CakeRequest(),
new CakeResponse()
);
Go to your app/Config/core.php and change
Configure::write('debug',0);
to
Configure::write('debug',2);
Visit the same page you will get the detail about the error
There's a magic function to autoload classes (__autoload), I want to know if there a way to load a file without a class.
Something like this:
require ('example_file'); // Trigger __autoloadfiles function
function __autoloadfiles($filename)
{
$files = array(
ROOT . DS . 'library' . DS . $filename. '.php',
ROOT . DS . 'application' . DS . $filename . '.php',
ROOT . DS . 'application/otherfolder' . DS . $filename. '.php'
);
$file_exists = FALSE;
foreach($files as $file) {
if( file_exists( $file ) ) {
require_once $file;
$file_exists = TRUE;
break;
}
}
if(!$file_exists)
die("File not found.");
}
You can define own function for requiring:
function require_file($file) {
// your code
}
and then call it
require_file('file');
I guess that there is no way to overload require function.
Trying to autoload classes from <root>/incl/classes folder.
The problem is, when I call some class for ex. ip like that
$ip= new ip();
$ip=$ip->get();
PHP gives error message Notice: Undefined variable: path . But in fact file already exists
I'm declaring all various paths at the top of page.
define("ds", DIRECTORY_SEPARATOR);
$path = array();
$path['root'] = $_SERVER['DOCUMENT_ROOT'];
$path['common'] = $path['root'] . ds . "common";
$path['design'] = $path['root'] . ds . "design";
$path['contents'] = $path['root'] . ds . "contents";
$path['content_images'] = $path['root'] . ds . "content" . ds . "images";
$path['design_images'] = $path['root'] . ds . "design" . ds . "images";
$path['blocks'] = $path['contents'] . ds . "blocks";
$path['includes'] = $path['root'] . ds . "incl";
$path['pages'] = $path['contents'] . ds . "pages";
$path['classes'] = $path['includes'] . ds . "classes";
$files = glob("common" . ds . "*.php");
array_unshift($files, $path['common'] . ds . "settings.php", $path['common'] . ds . "db.php");
foreach ($files as $filename)
require_once $filename;
//Auto loading classes
function __autoload($class_name) {
if (file_exists($path['classes'] . ds . $class_name . '.php')) {
require_once($path['classes']. ds . $class_name . '.php');
} else {
die($path['classes'] . ds . $class_name . '.php');
}
}
For testing purposes added die($path['classes'] . ds . $class_name . '.php'); line. It outputs \ip.php. I wonder, why it doesn't echo $path['classes'] even if I declared it before ?
It's a scoping issue. Your $path variable doesn't exist in your autoload function. It is a global variable, and you need to explicitly "invite" it:
function __autoload($class_name) {
global $path;
You should actually have gotten a notice about this.