PHP: calling require_once in constructor function - php

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;
}
}

Related

How to trigger a 404 page if the URL doesn't match any controller or function

You need to add a 404 error page. This page should be called if the URL does not match any controller or function that is in the project.
please help me I don't know how to do this
App.php
<?php
class App {
protected $controller = 'Home';
protected $method = 'index';
protected $params = [];
public function __construct() {
$url = $this->parseUrl();
if(file_exists('app/controllers/' . ucfirst($url[0] ?? null) . '.php')) {
$this->controller = ucfirst($url[0]);
unset($url[0]);
}
require_once 'app/controllers/' . $this->controller . '.php';
$this->controller = new $this->controller;
if(isset($url[1])) {
if(method_exists($this->controller, $url[1])) {
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl() {
if(isset($_GET['url'])) {
return explode('/', filter_var(
rtrim($_GET['url'], '/'),
FILTER_SANITIZE_STRING
));
}
}
}
please help me I don't know how to do this

How to create an object from a class inside another class

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'], "/"));
}
}
}

How i can create child view for parent view in PHP MVC?

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

PHP MVC - return call_user_func_array() in a __construct

I am working on a MVC site, but I have a problem. I can not return anything from my controllers. I can do things like $this->someMethod() but not return "hello" I think it has to do with call_user_func_array() So how do I fix this?
My code:
Router:
Class App
{
protected $controller = 'home';
protected $method = 'index';
protected $parameters = [];
public function __construct()
{
$url = $this->Url();
if(file_exists('../app/controllers/'. $url[0] .'.php')){
$this->controller = $url[0];
unset($url[0]);
}
require_once '../app/controllers/'. $this->controller .'.php';
$this->controller = new $this->controller;
if (isset($url[1])){
if (method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
$this->parameters = $url ? array_values($url) : [''];
call_user_func_array([$this->controller, $this->method], $this->parameters);
}
public function Url()
{
if(isset($_GET['url'])){
$url = filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL);
$exploded_url = explode('/', $url);
return $exploded_url;
}
}
}
Public Index.php:
<?php
require_once '../app/core/init.php';
$app = new App;
A controller:
class Home extends Controller
{
public function index($name)
{
//this works.
$this->view('index');
//this does not work.
return "hello";
}
}
So how do I solve it, so the return works too?

using a static class with properties in php

I'm trying to get the selected language to appear in a link with the function buildMenu().
I would like to use it as a static function so I can call it in my header template. If I call the function in the init() function it all works fine, however, when I try to use it as a static function, nothing works anymore. I've tried everything I know, so it seems my knowledge of php ends there :)
Any of you got any tips? Thanks in advance!
class bootstrap {
static public $lang;
static public $class;
static public $method;
public function init(){
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
//Set values on startup
if($url[0] == NULL) {$url[0] = 'nl';}
if($url[1] == NULL) {$url[1] = 'index';}
if(isset($url[0])) {$this->lang = $url[0];}
if(isset($url[1])) {$this->class = $url[1];}
if(isset($url[2])) {$this->method = $url[2];}
$this->loadClass();
}
public function loadClass(){
$filename = 'libs/' . $this->class . '.php';
if(file_exists($filename)){
$newController = new $this->class($this->lang, $this->class, $this->method);
$newView = new view($this->lang, $this->class, $this->method);
} else {
$newclass = new error($this->lang, $this->class, $this->method);
}
}
public function buildMenu(){
echo '<li>Foto</li>';
}
/*
* Set paths
*/
public static function root(){
echo "http://localhost/testing/";
}
}
You are using the object operator (->) instead of the scope resolution operator (::) that is used to reference class constants and static properties or methods.
See here for an explanation of the static keyword and working with static properties.
Update your code to this:
class bootstrap{
static public $lang;
static public $class;
static public $method;
public function init(){
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
//Set values on startup
if($url[0] == NULL) {$url[0] = 'nl';}
if($url[1] == NULL) {$url[1] = 'index';}
if(isset($url[0])) {self::$lang = $url[0];}
if(isset($url[1])) {self::$class = $url[1];}
if(isset($url[2])) {self::$method = $url[2];}
$this->loadClass();
}
public function loadClass(){
$filename = 'libs/' . self::$class . '.php';
if(file_exists($filename)){
$newController = new self::$class(self::$lang, self::$class, self::$method);
$newView = new view(self::$lang, self::$class, self::$method);
} else {
$newclass = new error(self::$lang, self::$class, self::$method);
}
}
public static function buildMenu(){
echo '<li>Foto</li>';
}
public static function root(){
echo "http://localhost/testing/";
}
}
As #elclanrs has already mentioned, how about changing the buildMenu() method to
public static function buildMenu(){
echo '<li>Foto</li>';
}
You can then call it using bootstrap::buildMenu().

Categories