Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
    
Which framework are you using??...and please do not respond with my own framework! – Hackerman 3 hours ago
    
@Hackerman: It's the Luuk777w framework. – AbraCadaver 3 hours ago
    
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? – S. Imp 3 hours ago
    
Where are you attempting to use the returned value??? – AbraCadaver 3 hours ago
    
@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 – Luuk777w 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.