I would like to get all of the base url of the site in my twig extension:
$this->service_container->get('request')->getBaseUrl()
gives me a lot but the host is missing...
another method maybe to get everything or one to get the host ?
Goal : have this :
http://my_host/request_base_url
$request = $this->getRequest();
$scheme = $request->getScheme();
$host = $request->getHttpHost();
$base = sprintf('%s://%s', $scheme, $host);
This is how im using this code to get complete base url .
public function test($request){
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath() ;
}
Related
I am having trouble to dynamic the url using codeigniter.
I need to display dynamic URL look like below.
http://example.com/[store-url]
I did that. But when i try to view non-dynamic url like
http://example.com/blog. It accesssing store page only.
So i have wrote in my routes like below and it is working.
$route['blog'] = "blog";
But here my problem is i am having lot pages such as /blog. all pages are pointing the stores pages only including admin control panel
There any solution for that without setting the routes
In the routes.php
First wirte the coding of Dynamic URL
like
include_once (APPPATH . 'helpers/inflector_helper.php');
$path = explode('/', $_SERVER['REQUEST_URI']);
if ($_SERVER['HTTP_HOST'] == 'www.expample.com') {
$controller = $path[1];
} else {
$controller = $path[2];
}
$route[$controller] = plural($controller) . "/view" . ucwords($controller);
$route[$controller . '/list'] = plural($controller) . "/view" . ucwords($controller);
$route[$controller . '/view/(:num)'] = plural($controller) . "/view" . ucwords($controller) . "/$1";
$route[$controller . '/view/(:num)/(:any)'] = plural($controller) . "/view" . ucwords($controller) . "/$1/$2";
I write my static url
$route['login'] = "authenticate_user/index";
$route['validate'] = "authenticate_user/validateUser";
$route['logout'] = "authenticate_user/logout";
I hope this should work.
Hello I'm currently working with php to generate a menu with a own build CMS system.
I'm making a dynamic link with : $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."/";
Than I'm adding . $row_menu['page_link'] from the database. At first it works perfect:
as example =
$row_menu['page_link'] = page2;
$url . $row_menu['page_link'];
it will return as example : http://example.com/page2
But when I click again, it adds page2 again like : http://example.com/page2/page2
How do i prevent this?
Thanks in advance!
Because at first time your $_SERVER['REQUEST_URI'] will be like http://example.com but when the user click on the link then the value of $_SERVER['REQUEST_URI'] would become http://example.com/page2.That's why it is appending two times.
Instead you can use HTTP_REFERER like
$url = $_SERVER['HTTP_REFERER'].$row_menu['page_link'];
Considering that your $_SERVER['HTTP_REFERER'] will results http://example.com.Also you can try like
$protocol = 'http';
$url = $protocol .'//'. $_SERVER['HTTP_HOST'] .'/'. $row_menu['page_link'];
REQUEST_URI will give you whatever comes after example.com, so leave that out all together.
$url = $_SERVER['HTTP_HOST'] . "/" . $row_menu['page_link'];
You can find a full list of the $_SERVER references here.
Try this:
$requested_uri = $_SERVER['REQUESTED_URI'];
$host = $_SERVER['HTTP_HOST'];
$uri_segments = explode('/',$requested_uri);
$row_menu['page_link'] = 'page2';
if($row_menu['page_link'] == $uri_segments[sizeof($uri_segments)-1]) {
array_pop($uri_segments);
}
$uri = implode('/',$uri_segments);
$url = 'http://'.$host.'/'.$uri.'/'.$row_menu['page_link'];
echo $url;
i´m trying to include some code from the own server via an api / brigde / whatever...
class ZKBrigde {
private $_url;
public function __construct() {
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$script = explode('/', $script);
array_pop($script);
$this->_url = "http://" . $host . implode('/', $script) . '/zk/';
}
Here i´m looking for the main url of the target framework.
In an request im using this (example, $items is an integer):
return file_get_contents($this->_url . "brigde/acp/page/changelog/$items");
First, this has worked. In some cases if i have an error in my code, it don´t work, returns false. OK, i´ve fixed the errors.
Now, changed some code of the backend, it doesnt work.
If i type the URL in my browser, i get the required result, no errors.
With this now i always get the result "FALSE".
How to check what is wrong?
Any hints to make it better?
(It should be an API / Brigde to my CMS)
lg., Kai
Try this:
$this->_url = trim("http://" . $host . implode('/', $script) . '/zk/');
And try if the URL are clear and in a good format!
class ZKBrigde {
private $_url;
public function __construct() {
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$script = explode('/', $script);
array_pop($script);
$this->_url = trim("http://" . $host . implode('/', $script) . '/zk/');
$open = open("test.txt", "w");fwrite($open, $this->_url);fclose($open);
}
Take a look in test.txt when you've tested the script.
According to PHP's documentation if your path has special characters and you are trying to open an URI, then you need to use urlencode() function. Does your path have special characters? I'd use urlencode to be in the safe side.
http://us1.php.net/file_get_contents
I have an example link here:
http://mydomain.com/myapp1/index.php/image/index/album/check+picture/id/1
Now I'm trying to retrieve the "1" at the very end of the url.
So far, I've tried the following code:
$id = $_GET['id'];
But it is not working. I was used to the url having the index.php?id=1 Syntax but I'm not entirely sure how to get this one working.
UPDATE
Before accepting an answer, I wanted to add this script I used to get the entire URL of the current page:
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;
echo $currentUrl;
When it echoes, it only prints out:
http://www.mydomain.com/myapp1/index.php?
Just do this :
echo basename($_SERVER['REQUEST_URI']);
Something like this should work for you:
$id = substr($url, strrpos( $url, '/' )+1);
Get YII Documentation...
$id = Yii::app()->request->getQuery('id')
Or
$id = Yii::app()->getRequest()->getQuery('id');
Possible duplicate
http://stackoverflow.com/questions/2760869/yii-framework-controller-action-url-parameters
try this....
$url = "http://mydomain.com/myapp1/index.php/image/index/album/check+picture/id/1";
$value = substr(strrchr(rtrim($url, '/'), '/'), 1);
Ref
In Yii Framework you can get value like this...
$id = Yii::app()->getRequest()->getQuery('id');
OR
$id = Yii::app()->request->getParam('id');
I am struck in getting the URI in my wordpress application and lack of PHP knowledge is making my progress slow.
I have this URL
http://abc.com/my-blog/abc/cde
i need to create a URL something like
http://abc.com/my-blog/added-value/abc/cde
where http://abc.com/my-blog is the URL of my wordpress blog which i can easily get using following method
home_url()
i can use PHP $_SERVER["REQUEST_URI"] to get request URI which will come up as
/my-blog/abc/cde
and than i have no direct way to add value as per my requirement
is there any way to achieve this easily in PHP or Wordpress where i can get following information
Home URL
Rest part of the URL
so that in end i can do following
Home-URL+ custom-value+Rest part of the URL
My point of Confusion
On my local set up $_SERVER["REQUEST_URI"] is giving me /my-blog/abc/cde, where /my-blog is installation directory of wordpress and i can easily skip first level.
On production server its not same as /my-blog will not be part of the URL.
Very briefly:
<?php
$url = "http://abc.com/my-blog/abc/cde";
$parts = parse_url($url);
$path = explode("/", $parts["path"]);
array_splice($path, 2, 0, array("added-part")); //This line does the magic!
echo $parts["scheme"] . "://" . $parts["host"] . implode("/",$path);
OK, so if $addition is the bit you want in the middle and $uri is what you obtain from $_SERVER["REQUEST_URI"] then this..
$addition = "MIDDLEBIT/";
$uri = "/my-blog/abc/cde";
$parts = explode("/",$uri);
$homeurl = $parts[1]."/";
for($i=2;$i<count($parts);$i++){
$resturl .= $parts[$i]."/";
}
echo $homeurl . $addition . $resturl;
Should print:
my-blog/MIDDLEBIT/abc/cde/
You might want to use explode or some other sting function. Some examples below:
$urlBits = explode($_SERVER["REQUEST_URI"]);
//blog address
$blogAddress = $urlBits[0];
//abc
$secondPartOfUri = $urlBits[1];
//cde
$thirdPartOfUri = $urlBits[2];
//all of uri except your blog address
$uri = str_replace("/my-blog/", "", $_SERVER["REQUEST_URI"]);
This is a reliable way to get current url in PHP .
public static function getCurrentUrl($withQuery = true)
{
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === false ? 'http' : 'https';
$uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return $withQuery ? $uri : str_replace('?' . $_SERVER['QUERY_STRING'], '', $uri);
}
You can store the home url in a variable, using wordpress, using get_home_url()
$home_url = get_home_url();
$custom_value = '/SOME_VALUE';
$uri = $_SERVER['REQUEST_URI'];
$new_url = $home_url . $custom_value . $uri;