Header command in PHP

Asked

Viewed 405 times

-1

I am trying to create a simple MVC system in which I have the following folder structure:

Arquivos de Código-Fonte - Pasta
  Servlet - Pasta
     UserServlet.class.php
  Controller - Pasta
     UserController.class.php
  Model - Pasta
     DAO - Pasta
        UserDAO.class.php
     VO - Pasta
        UserVO.class.php
  View - Pasta
    userForm.php
  index.php

I need to make mine index.php call the class Servlet, for that I put the following code:

<?php
    header('Location : Servlet/UserServlet.class.php?action=goHome');
?>

But the problem is that nothing happens and I need to call the class Servlet for in which I will handle which method I will use. If anyone knows a solution, a way for me to be able to make an HTTP request at Servlet will help me a lot.

  • Does it give any error ? Note that calling the function header cannot be done after it has been written html on the page, otherwise it will not work.

  • Are you just declaring the class or also instantiating it in the script itself? because if you said the operation is not executed, I imagine you wait for the script to do something, then there must be the class instance Servlet being called there...

  • You want to call the class to work with it or redirect the page to the class?

1 answer

0

You will not be able to do this with header('Location: '), pq when working with classes need to instantiate an object and call a method.

You can do it like this:

require 'Servlet/UserServlet.class.php';

$class = 'UserServlet';
$method = 'goHome';

$class = new $class;
call_user_func(array($class, $method));

A few years ago I created a project similar to this idea, called the Jiraya Framework, and used to work with small projects using MVC and friendly urls, take a look https://github.com/evandrobalmant/jiraya-mvc/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.