31

i have multiple bootstrap tabs where each one do different action from others tabs for exmaple

app-url/users#creat-admin-users-tab app-url/users#creat-regular-users-tab

Is there any way in Laravel to get full url including the #tab-name

Thanks for your time.

Bharat Geleda
  • 2,692
  • 1
  • 23
  • 31
Muhammad Atallah
  • 976
  • 2
  • 12
  • 34

3 Answers3

82

Laravel has functionality to return you the current url. It is all specified in this page: http://laravel.com/api/5.0/Illuminate/Http/Request.html

What you are looking for is Request::fullUrl().

Let's say I'm on http://laravel.dev/test?test=1, here are the methods and the results:

Request::fullUrl()
// Returns: http://laravel.dev/test?test=1
Request::url()
// Returns: http://laravel.dev/test
Request::path()
// Returns: test
Request::root()
// Returns: http://laravel.dev 
Andrius
  • 5,934
  • 20
  • 28
  • 1
    I used it before it's return all url but without `#tab-name` – Muhammad Atallah Nov 13 '15 at 07:39
  • 2
    The server does not know what content after *#*, it's only for the user. You have to append it yourself. – Andrius Nov 13 '15 at 07:39
  • 1
    the all tabs including in one route using `Route::match` so i want to check what the current tab to do the proper action i think the best way in this case is `$request->isMethod('')` but i don't know what i do if there multiple post or put method . – Muhammad Atallah Nov 13 '15 at 07:56
  • 2
    You can't check `#` values in the backend. You can do what with [javascript](http://stackoverflow.com/questions/19491336/get-url-parameter-jquery). – Andrius Nov 13 '15 at 08:00
  • @JustUser if you have separate `
    ` on each tab, you can put a kind of tab identifier into a hidden field of this form. If it's one large form, spread on all your tabs, you could still use the hidden field, but you'll need to update its value (current tab ID) using JavaScript.
    – pilat Aug 22 '17 at 07:57
  • @Andrius let's say http://whitelabel_eb.test/users/login?next=projects/3/interest?id=1&source=eoi this is my URL I want to go to next URL but all I am getting is projects/3/interest?id=1 after that it's not capturing anything. – iamsujit Mar 09 '18 at 10:06
  • For me, `url()->full()` was helpful, in addition to `\Illuminate\Support\Facades\Request::fullUrl()`. https://laravel.com/docs/5.6/helpers#method-url – Ryan May 14 '18 at 17:46
5

Check the following

$_SERVER['HTTP_HOST'] => Host name from the current request.

$_SERVER['HTTP'] => Set to a non-empty value if the protocol is HTTP

$_SERVER['HTTPS'] => Set to a non-empty value if the protocol is HTTPS

$_SERVER["SERVER_PORT"] => Server port. Default is: 80

$_SERVER['REQUEST_URI'] => The URI to access this page;

  • WARNING: bad idea. Anything starting with 'HTTP_' is sent by the client, do not rely on it. – AbiusX Dec 21 '18 at 20:26
5

It's true - the only way to get the "#" for now is to use js e.g. like this:

var hash = window.location.hash;
var tabName1 = '#creat-admin-users-tab';
var tabName2 = '#creat-regular-users-tab';
if (hash.indexOf(tabName1) !== -1) console.log("It's creat-admin-users-tab");
else if (hash.indexOf(tabName2) !== -1) console.log("It's creat-regular-users-tab");

And this can help you for the other parts of the url (add it in your route file):

use Illuminate\Http\Request;

Route::get('/app-url/users', function (Request $request) {
    $fullUrl = $request->fullUrl();
    var_dump($fullUrl);
});
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39