Creating links within PHP views

Asked

Viewed 47 times

1

I’m studying the MVC architecture watching videos and looking for examples on the Internet, I was able to implement the three layers and map the routes of my system, but I was wondering how to create links within the views. I don’t know if I should just call the files directly, or if I should use the controllers and route system for this.

1 - Creating direct links from archives

<li style="margin-top: -20px;"><a href="../login.php">Entrar</a></li>

2 - By route (I believe it is not the correct form but it works in my case)

<li style="margin-top: -20px;"><a href="login">Entrar</a></li>

3 - using controller (I don’t know how to do, I must instill a controller inside href ?)

<li style="margin-top: -20px;"><a href="     ">Entrar</a></li>

Class Action

<?php
namespace SON\Controller;

class Action{

    protected $view;
    protected $action;

    public function __construct(){
      $this->view = new \stdClass;
   }

    public function render($action, $layout=true){
        $this->action = $action;
        if($layout == true && file_exists("../App/Views/Template/layout.phtml")){

            switch ($action) {
                case "login":
                    include_once '../App/Views/Template/layoutLogin.phtml';
                    break;
                case "sobre":

                    break;
                case "home":
                include_once '../App/Views/Template/layoutHome.phtml';
                    break;
                default:
                include_once '../App/Views/Template/layout.phtml';
            }

        }
        else{
            $this->content();
        }
    }

    public function content(){
        $atual = get_class($this);
        $singleClassName = strtolower(str_replace("App\\Controllers\\","",$atual));
        include_once '../App/Views/'.$this->action.'.phtml';
    }

}

?>
  • how’s the main project template? inside it you have some code to render the other views or in each view inside it you have html head body etc?

  • I made an action class responsible for this process, I will update the code

  • Still doubted, inside these layoutHome files, layoutLogin you have the complete structure of an html? you load the views inside the body of your default template or each layout of it has a full html?

  • in each of these layout I have an HTML structure with head, header and footer, the contents of the pages stay in other files, to call it using <?php echo $this>content();?>

  • The number two format is ideal, you should follow using it whenever you can!

  • But analyzing how you built the structure of views and template I see that either you have more than one theme on the system or you are not able to create a base template! I believe that the right thing is inside the body of the base template you load your content from the page clicked! There are several ways to do this I advise you to give a googlada to have examples of route system to base on, but the easiest way is by using a $_GET you would have to adapt your code but take the parameter by the same url Ex: this would be your home link /index.php?page=home.

  • I created some different layouts because in case there is need, but in fact I do not know very well how to create a template, thanks for the help I will research more about

  • depends on the structure of your website.

Show 3 more comments
No answers

Browser other questions tagged

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