Problems with the structure of links in MVC

2

I'm working on an MVC project and I'm having trouble redirecting pages. The project is based on controllers and actions , which have the proper functions that call these files and call their methods, in this case actions.

This is the HTML of the menu:

<li class="button"><a href="">To view this page in English</a></li>
<li class="button"><a href="apresentacao">Apresentação</a></li>
<li class="button"><a href="objetivos">Objetivos</a></li>
<li class="button"><a href="participantes">Participantes</a></li>
<li class="button"><a href="pesquisa">Pesquisas e Projetos</a></li>

When the URL looks like this:

http://localhost/pasta_raiz/   ou
http://localhost/pasta_raiz/index

When you click on a link in the menu, for example, the presentation redirects correctly to the following link:

http://localhost/pasta_raiz/apresentacao

But when URLs are explicit action , that is:

http://localhost/pasta_raiz/index/index

By clicking the link presentation, I'm directed to:

http://localhost/pasta_raiz/index/apresentacao

Being that the correct one would be:

http://localhost/pasta_raiz/apresentacao

And when I put in the addresses href="/apresentacao" I'm forwarded to:

http://localhost/apresentacao

Resulting in a page not found . I do not know what to do, what could be wrong?

    
asked by anonymous 11.11.2014 / 14:09

3 answers

4

You need to define a function with the base URL of your application. for example:

<?php

function site_url($uri = null) {
   if($uri) {
      return "http://localhost/pasta_raiz/$uri";
   }

   return "http://localhost/pasta_raiz";
}

So in every link you'll create, you'll call:

<a href="<?php echo site_url('apresentacao'); ?>">apresentacao</a>

What happens, when you pass a url on <a href="apresentacao">aprensentação</a> , the browser will always retrieve the current page + the link that is in the href="" tag. to solve this you need to pass the url complete, using a simple function like I mentioned above solves the problem.!

    
11.11.2014 / 14:39
4

The function below is an adaptation to my Request :: getBaseUrl () and works almost the same way as the one proposed by @Wellington but uses server variables for the first immutable half of the final string:

function getBaseUrl( $basepath = NULL ) {
    return sprintf( 'http://%s/%s/', $_SERVER['HTTP_HOST'], $basepath );
}

var_dump( getBaseUrl( 'somedir' ) . 'myfile.php' );

Results in:

http://localhost:8080/somedir/myfile.php

By using the $ _ SERVER ['HTTP_HOST'] server variable, if there is any port, such as the internal PHP 5.4+ server, it is already automatically added, requiring no modifications.

    
11.11.2014 / 17:35
3

Another way to solve this would be to put the TAG in the header of your site, eg:

<base href="http://localhost/pasta_raiz/"/>

So your link to controller would be

<a href="apresentacao" title="...">Apresentação</a>

With action

<a href="apresentacao/acao" title="...">Ação</a>

This method also solves problems with CSS, JS, Images, and etc ...

    
11.11.2014 / 18:26