I have a problem creating an object from another class inside the constructor of my kernel class, it gives a fatal error which says can not find the class, anybody can help please? I do not know what is wrong?
namespace App\Http;
use App\Http\Config;
use App\Controllers;
class kernel{
protected $controller = "HomeController";
protected $action = "index";
protected $params=[];
public function __construct(){
$url = $this->ParseUrl();
$format_url = ucfirst($url[0]) . "Controller";
if (file_exists(Config::CONTROLLERS_PATH . $format_url . ".php")) {
$this->controller = $format_url;
}
// $path = Config::CONTROLLERS_PATH . $this->controller . ".php";
// echo "path: $path";// path is correct
require_once (Config::CONTROLLERS_PATH . $this->controller . ".php");
// $include_file = get_required_files();
// var_dump($include_file);//require_once is working
//here is the problem
$this->controller = new $this->controller;
}
public function ParseUrl(){
if (isset($_GET['url'])) {
return explode("/", rtrim($_GET['url'], "/"));
}
}
}
Related
When i type this address:
http://localhost/gamelvl/world-of-tanks/tankguide/
I can enter the tankguide view. Now inside this folder is another folder called ussr but when I create a controller and model for ussr I can not enter it with this address:
http://localhost/gamelvl/world-of-tanks/tankguide/ussr/
Now, any one can give me instructions on whether this is the right thing or other solutions for this?
routing app
<?php
class App
{
public $controller = 'index';
public $method = 'index';
public $params = [];
function __construct()
{
if (isset($_GET['url'])) {
$url = $_GET['url'];
$url = $this->parseUrl($url);
$this->controller = $url[0];
unset($url[0]);
if (isset($url[1])) {
$this->method = $url[1];
unset($url[1]);
}
$this->params = array_values($url);
}
$controllerurl = 'controllers/' . $this->controller . '.php';
if (file_exists($controllerurl)) {
require($controllerurl);
$object = new $this->controller;
$object->model($this->controller);
if (method_exists($object, $this->method)) {
call_user_func_array([$object, $this->method], $this->params);
}
}
}
function parseUrl($url)
{
filter_var($url, FILTER_SANITIZE_URL);
$url = rtrim($url, '/');
$url = explode('/', $url);
return $url;
}
}
?>
tankguide controller
<?php
class Tankguide extends Controller
{
function index()
{
$this->view('tankguide/index');
}
}
?>
core controller
<?php
class Controller
{
function __construct()
{
}
function view($viewUrl,$data=[])
{
require('header.php');
require('views/' . $viewUrl . '.php');
require('footer.php');
}
function model($modelUrl)
{
require('models/model_' . $modelUrl . '.php');
$classname = 'model_' . $modelUrl;
$this->model = new $classname;
}
}
?>
project structure
if (method_exists($object, $this->method)) will return false for the given url, because it is looking for a method called ussr on the Tankguide class. Either create a ussr method on that class
require_once in constructor function, is not working I also tried include and require. file_exists function is working fine.
Here is my code:
<?php
class App {
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
public function __construct() {
$url = $this->parseUrl();
//print_r($url);
if(file_exists(__DIR__.'/../controllers/'. $url[0] . '.php')) {
$this->controller = $url[0];
unset($url[0]);
}
require_once(__DIR__.'/../controllers/Home.php');
echo $this->controller;
}
}
I am seeing some weird error on my client website. It's code someone else made for him for his application. The error message is saying that $this when not in object context but the Class has been extended where from App.
Please help out.
Error
Fatal error: Using $this when not in object context in contactController.php on line 6
contactController.php
class contactController extends App{
public function index(){
$this->view('content'); //error mmessage is pointing here
}
}
app.php
class App{
public $controller;
public $method;
public $params = [];
public function view( $file ){
include( site_path() . '/views/' . $file . '.php' );
}
public function model( $file ){
require_once( site_path() . '/models/' . $file . '.php' );
}
public function engine(){
global $core_current_ControllerMethod, $core_current_controller, $core_current_method;
//Get the current controller and method from a helper function
$get_wc_cm = get_wc_cm();
//Assign it to the global variable
$core_current_ControllerView = $get_wc_cm;
//Seperate the controller and method
$cm = explode('#', $get_wc_cm);
$controller = !empty($cm[0])? $cm[0]: null; // This is the controller
$method = !empty($cm[1])? $cm[1]: null; // This is the method
//Assign it to the global varaible
$core_current_controller = $controller;
$core_current_method = $method;
//Assign it to the class variable
$this->controller = $controller;
$this->method = $method;
$ControllerFile = site_path(). '/controllers/' . $this->controller . '.php';
if( file_exists($ControllerFile) ){
require_once($ControllerFile);
new $this->controller;
$callback = is_callable(array($this->controller, $this->method), false);
if( $callback ){
call_user_func_array([$this->controller, $this->method], [$this->params]);
}
}
}
}
$app = (new App)->engine();
Try to change :
class contactController extends App{
public function index(){
$this->view('content'); //error mmessage is pointing here
}
}
To :
class contactController extends App{
public function index(){
parent::view('content'); //error mmessage is pointing here
}
}
All,
I am reading the following article on a lightweight PHP dynamic front controller: http://www.w3style.co.uk/a-lightweight-and-flexible-front-controller-for-php-5
Here is the code:
index.php
<?php
define("PAGE_DIR", dirname(__FILE__) . "/pages");
require_once "FrontController.php";
FrontController::createInstance()->dispatch();
FrontController.php
<?php
class FrontController {
public static function createInstance() {
if (!defined("PAGE_DIR")) {
exit("Critical error: Cannot proceed without PAGE_DIR.");
}
$instance = new self();
return $instance;
}
public function dispatch() {
$page = !empty($_GET["page"]) ? $_GET["page"] : "home";
$action = !empty($_GET["action"]) ? $_GET["action"] : "index";
//e.g. HomeActions
$class = ucfirst($page) . "Actions";
//e.g. pages/home/HomeActions.php
$file = PAGE_DIR . "/" . $page . "/" . $class . ".php";
if (!is_file($file)) {
exit("Page not found");
}
require_once $file;
$actionMethod = "do" . ucfirst($action);
$controller = new $class(); // I DON'T UNDERSTAND WHAT THIS DOES...
if (!method_exists($controller, $actionMethod)) {
exit("Page not found");
}
//e.g. $controller->doIndex();
$controller->$actionMethod();
exit(0);
}
}
pages/guestbook/GuestbookActions.php
<?php
class GuestbookActions {
public function doIndex() {
echo "Index action called...";
}
public function doCreatePost() {
echo "CreatePost action called...";
}
}
In the front controller class, could someone explain to me what $controller = new $class(); does? I don't understand it. It seems to be creating a class on the fly? In the example above, $class is a string with a value like "HomeActions". So $controller would be a new instance of a class named "HomeActions", but those are not defined anywhere. I'm confused.
Many thanks,
JDelage
$controller = new $class();
That does indeed create a new object of the type contained in $class, so it is equivalent to $controller = new HomeActions() in your example. From the manual:
If a string containing the name of a class is used with new, a new instance of that class will be created
The classes are not all present initially. However, the necessary one is loaded dynamically:
$file = PAGE_DIR . "/" . $page . "/" . $class . ".php";
if (!is_file($file)) {
exit("Page not found");
}
require_once $file;
require_once loads the file which presumably contains the class definition, so you can create the object as shown above.
The example request in the article is to http://localhost/index.php?page=guestbook&action=index, so $class would be GuestbookActions, which is defined in the third code sample.
I have some class
/library/QPF/Loader.php
namespace QPF;
class Loader
{
protected static $loader = null;
public function __construct()
{
spl_autoload_register('QPF\Loader::_autoload');
}
public static function init()
{
if (null === self::$loader) {
self::$loader = new Loader();
}
return self::$loader;
}
public function _autoload($class)
{
//if (class_exists($class)) return true;
$classFile = str_replace('\\', '/', $class) . '.php';
require_once $classFile;
if (!class_exists($class)) throw new Extension('Not found class');
}
}
/library/Version.php
namespace QPF;
class Version
{
public function getVersion()
{
return '0.1';
}
}
/public/index.php
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../library');
define('APPLICATION_PATH', dirname(__FILE__) . '/../application');
require_once 'QPF/Loader.php';
QPF\Loader::init();
echo 'start';
use QPF;
$v = new QPF\Version();
var_dump($v);
echo 'ss';
Version class loading, but var_dump show what it's empty class without function getVersion();
startobject(QPF\Version)#2 (0) { } ss
Methods do not show up in var_dump or print_r output, as they are not part of the state of the object. Try calling the method; it should work as expected.