1

I am using Lemp. This code

$url = $_SERVER['PHP_SELF'];

$url is set to the value www.mydomain.com/controller/action.

This is what I expected, since it is the url that the code is being executed.

but this

$pagina = $i + 1;
$url = $_SERVER['PHP_SELF'];
$url .= "/{$pagina}" ;

I get $url set to www.mydomain.com.br/1

My controller and action part of url are gone.

Isn't $_SERVER['PHP_SELF'] a constant.

I forgot to say my intention, I want to get the current url concanate a value that will be used as route parameter: wwww.mydomain.com.br/controller/action/my_route_parameter_concatenated

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • 1
    Don't use $_SERVER['PHP_SELF'] with Laravel. It has very nice URL helpers, check them out. – Robo Robok Mar 05 '17 at 19:26
  • I am "upgrading" a site that was originaly written in raw PHP and I was gotten carried away by the already implemented solution. Of course if I have a route and want to pass parameters, link_to_route seems a good way to go. – Diego Alves Mar 05 '17 at 19:59
  • There are several possibilities to get URLs in Laravel. My favorite is to use named routes and then `route('foo')`, because that's the most transparent way of linking. The name will stay the same no matter what. My second favorite way is to link by controller's action, like `action('HomeController@welcome');` - this one is awesome too, just a little less flexible on edge cases. – Robo Robok Mar 05 '17 at 20:02

3 Answers3

2

If I'm understanding your question correctly, you could use the url() helper function for this:

url('controller/action/my_route_parameter_concatenated')

https://laravel.com/docs/5.3/helpers#method-url

Hope this helps!

Rwd
  • 34,180
  • 6
  • 64
  • 78
0

Answers and commments led me to the right direction. Since was going to use $url to create an anchor tag, I just used link_to_route('paineldecontrole.anuncios', $pagina, [ $pagina]).

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
-1

because Yo're using Laravel then maybe use framework power? http://blog.netgloo.com/2015/07/15/lumen-get-the-full-url-with-the-query-string/ Laravel Request has many more useful tools.

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
barat
  • 1,040
  • 8
  • 14