Link structure problems in MVC

Asked

Viewed 226 times

2

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

This is the HTML 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 is like this:

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

By clicking on a link in the menu, for example, presentation the redirect occurs correctly to the following link:

http://localhost/pasta_raiz/apresentacao

But when the Urls are with the action explicit, that is to say:

http://localhost/pasta_raiz/index/index

By clicking on link presentation, I’m referred to:

http://localhost/pasta_raiz/index/apresentacao

When the right thing would be:

http://localhost/pasta_raiz/apresentacao

And when I put it in the addresses href="/apresentacao" I am referred to:

http://localhost/apresentacao

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

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 on every link you create, you call for:

<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 recover the current page + the link that is in the tag href="". to solve this you need to pass the full url using a simple function as I mentioned above solves the problem.!

4

The function below is an adaptation to mine Request::getBaseUrl() and works almost the same as the @Wellington proposal 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

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

  • Wouldn’t it be nice to put one rtrim in order to normalize the number of bars? rtrim(sprintf( 'http://%s/%s/', $_SERVER['HTTP_HOST'], $basepath ), '/');

  • 1

    As I made a mere adaptation when I had answered, I made it as simple as possible. If you check the original link source code you will see that there are other things besides a mere sprintf().

3

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

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

So your link to the controller would be

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

With action

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

This method even solves problems with include of CSS, JS, Images and etc...

  • 1

    Interesting. This will certainly decrease the lengths of my HTML lines

Browser other questions tagged

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