0

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?

9
  • Which framework are you using??...and please do not respond with my own framework! Commented Jan 9, 2017 at 20:46
  • @Hackerman: It's the Luuk777w framework. Commented Jan 9, 2017 at 20:50
  • You have all the code you need to answer your own problem I think. Your Router class makes the call to call_user_func_array and this is where the value would be returned. If you wanted to do something with the return value, that would be the place. Are you sure you really mean "return" a value? Or do you mean output a value? Commented Jan 9, 2017 at 20:51
  • Where are you attempting to use the returned value??? Commented Jan 9, 2017 at 20:53
  • @Hackerman Its for a school project, and they do not allow the use of frameworks for this project. So I HAVE TO make my own Commented Jan 9, 2017 at 20:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.