I am trying to get the base URL for the project in Yii 2 but it doesn't seem to work. According to this page you used to be able to do:
Yii::app()->getBaseUrl(true);
In Yii 1, but it seems that that method in Yii 2 no longer accepts a parameter?
I've tried doing it without true, such as:
Yii::$app->getBaseUrl();
But it just returns empty.
How can you do this in Yii 2?
To get base URL of application you should use yii\helpers\Url::base() method:
use yii\helpers\Url;
Url::base(); // /myapp
Url::base(true); // http(s)://example.com/myapp - depending on current schema
Url::base('https'); // https://example.com/myapp
Url::base('http'); // http://example.com/myapp
Url::base(''); // //example.com/myapp
Url::home() should NOT be used in this case. Application::$homeUrl uses base URL by default, but it could be easily changed (for example to https://example.com/myapp/home) so you should not rely on assumption that it will always return base URL. If there is a special Url::base() method to get base URL, then use it.
My guess is that you need to look at aliases.
Using aliases would be like:
Yii::getAlias('#web');
You can also always rely on one of these two:
Yii::$app->homeUrl;
Url::base();
To get base URL Yii2 using:
Url::home(true)
Use it like this:
Yii::$app->getUrlManager()->getBaseUrl()
More information on base, canonical, home URLs:
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
Try this:
$baseUrl = Yii::$app->urlManager->createAbsoluteUrl(['/']);
may be you are looking for this
Yii::$app->homeUrl
you can also use this
Url::base().
or this
Url::home();
I searched for a solution how we can do like in codeigniter, routing like
e.g.
base_url()
base_url('profile')
base_url('view/12')
Only way we can do that in Yii2
<?=Url::toRoute('/profile') ?>
You can reach your base URL by this:
Yii::$app->request->baseUrl
In yii 1 this code return the hostname
Yii::app()->getBaseUrl(true);
In yii2 the following
Yii::$app->getBaseUrl();
not exists as method of Yii::$app and it triggers an error with message
Calling unknown method: yii\web\Application::getBaseUrl()
You could use Request class that encapsulates the $_SERVER
Yii::$app->request->hostInfo
Try below code. It should work. It will return base URL name
use yii\helpers\Url;
Url::home('http') // http://HostName/
OR
Url::home('https') // https://HostName/
Related
I have a route like this:
Route::get('demo/{system?}', 'MonitorController#demo');
I am using it like so because I would like my url to look like so:
mysite.com/demo/spain-system
Where spain-system will be the variable I need to get.
Right now, I'm getting it like this:
public function demo($systemName = null){
}
But I would like to be able to access to it as if it were a URL parameter with Input::get('system') so I can access to it from other methods or even from other controllers such as BaseController.php.
Is there any way to achieve this?
I've played around with Route::input('system') but then it doesn't work when I pass it as a get parameter (in other Ajax calls and so on)
Update
In PHP we can get URL params by using the $_GET function and laravel provides the function Input::get() to do so as well.
If there were no routes in laravel, I would make use of .htaccess rewrite rules to change this:
mysite.com/demo/?system=spain-system
To this:
mysite.com/demo/spain-system
And I could still retrieve the variable system as a GET parameter by using $_GET["system"].
That's kind of what I would expect of laravel, but it seems it is just treating it as the parameter of the demo method and not really as a URL variable.
Is there any way to keep treating it as a URL variable and at the same time use it in a pretty URL without the ?system= ?
So you actually just want to get an url like this? mysite.com/demo/spain-system instead of mysite.com/demo/?system=spain-system? Laravel provides that by default?
Look, When you want to get the router variable {system?} to be accesible you'll need to do this:
In your router:
Route::get('demo/{system}', 'MonitorController#demo');
Then you have an controller where this noods to stand in:
public function demo($system)
{
//your further system
//You are be able to access the $system variable
echo $system; //just to show the idea of it.
}
When you now go to to localhost/demo/a-system-name/, You'll see a blank page with a-system-name.
Hope this helps, because your question is abit unclear.
I am using Yii.
if my url is http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
My real url (file path) is only http://localhost/evengee
How would I obtain, preferably in the view:
full url http://localhost/evengee/event/page/id/2/sfn+master/?tab=3
url without explicit parameters http://localhost/evengee/event/page/id/2/sfn+master/
I know I can split/explode str_replace by the ? and use $_SERVER. Would prefer to use
Yii native methods.
For:
full URL (http://localhost/even/page/id/2/?t=3) use
Yii::app()->request->getUrl(),
URL without parameters (http://localhost/even/page/id/2/ use
Yii::app()->request->getPathInfo().
Second one will give you the URL without schema, server and query string. This seems good enough.
To get the full URL, use the getRequestUrl() method of CHttpRequest
Yii::app()->request->getRequestUrl();
You can get the controller, module and action name of the current page from the CApplication methods
Yii::app()->getController()->id; //gets you the controller id
Yii::app()->getController()->getAction()->id; //gets you the action id
You can piece together a URL using the baseURL property of CApplication
Yii::app()->baseURL
The best and shorter way I found is:
Yii::app()->createAbsoluteUrl(Yii::app()->request->getPathInfo());
OR
$this->createAbsoluteUrl(Yii::app()->request->getPathInfo());
For Yii2, use \yii\helpers\Url::canonical().
Documentation: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#canonical()-detail
The following is a valid REST based URL that can be used to access a resource:
Using codeigniter, how can one access the parameter of 1 that was passed below.
I saw the above in a tutorial and have set up my code. However obviously:
$id = $this->input->get('id');
does not work.
Using $this->input->get('id') would suggest you are sending ?id=1 to the end of the URL. You can use $this->uri->segment(1) but that does not allow for paired uri segments.
If you use $this->get('id') which is a special REST_Controller method then it will pick up either. I did put that in the tutorial you got this image from :)
From the URI Class.
you can use $this->uri->segment(n)
Where n is the segment number you wish to retrieve .
In most web applications we need global var base_url. In cakephp to get base_url currently i put the following code on beforeRender method in app_controller.php
function beforeRender(){
$this->set('base_url', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}
Is there any alternative? Means is there any global variable available to get the base url rather than this?
Yes, there is. In your view, you may access:
<?php echo $this->webroot; ?>
Also, your host information is stored in the $_SERVER['HTTP_HOST'] variable in case you want that.
In your controller, if you want full URLs, use this:
Router::url('/', true);
Use anyone option below
<?php echo $this->Html->url('/');?>
<?php Router::url('/', true); ?>
<?php echo $this->base;?>
<?php echo $this->webroot; ?>
Define constant in Config/core.php as define("BASE_URL", "www.yoursite.com/"); and use BASE_URL anywhere in your project
and create a common helper with following functions
<?php
class CommonHelper extends AppHelper {
function get_url($url){
return BASE_URL.$url;
}
function get_src($url){
echo BASE_URL.$url;
}
}
?>
and use anywhere in project
$this->redirect($this->Common->get_url("login");
login
Don't forgot to include Common helper in controller
I recommend method 2 and 5 because they give complete url.
You may use
<?php echo Router::fullbaseUrl();?>
as well.
Refer http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html for more details.
Use Router::url('/', true) anywhere in your app.
Specifically in the View, you can use $this->Html->url('/', true) (or any other Helper in fact, the Helper::url method is inherited by all Helpers), which is just a wrapper for the above Router method.
In either case, the second true parameter causes it to return the full URL.
You can use
Router::fullBaseUrl()
If you have for example example.com/test and you want to ignore /test, you can use 'full' => false.
Also if you want to force ssl you can add '_ssl' => true.
i.e.
Router::fullBaseUrl(null, [ '_ssl' => true, 'full' => false]
Make sure you pass null as the first parameter as this is the base url in case you want to pass it.
note:
you need to import Router so you can use above function:
use Cake\Routing\Router
For most purposes I'd suggest using the CakePHP HtmlHelper to generate URLs, that way you won't need to worry about the base URL. The most user friendly way of getting the base URL in your view, however, would be <?php echo $html->webroot; ?>.
You can use FULL_BASE_URL constant.
I am using the Zend framework and what I need is to construct a url in my view. Normally in regular php code I'd just grab the GET Variable by using the global $_GET. However with Zend I'm setting it to clean URIs so :
?ac=list&filter=works&page=2
Looks like
index/ac/list/filter/works/page/2
In my view I'm setting a links cs such that if the GET variable filter equals works then the color of that link would be different and it would point to the same page only linked as so:
index/ac/list/filter/extra/page/2
And like wise I have a number of other links all which just one GET value - how do I set this up - I am using the Zend framework.
To access a request variable direct in the view you could do:
Zend_Controller_Front::getInstance()->getRequest()->getParam('key');
But as others have said, this is not a good idea. It may be easier, but consider other options:
set the view variable in the controller
write a view helper that pulls the variable from the request object
If you need to access a GET parameter from a view, i think you're doing it the wrong way.
I suggest that you set up a route with all your parameters, and then use $this->url from your view to render a valid and correct url.
Fore som more info, check out the following blog post (no, i'm not the author):
http://naneau.nl/2007/07/08/use-the-url-view-helper-please/
Edit:
If you want to be 'lazy', you can set a view parameter from your controller by doing $this->view->param = $this->_getParam('param'). You can then access param from your view by doing echo $this->param;. However, i do not recommend this.
To access the Request Object one way that is common is to save it in the Registry.
http://osdir.com/ml/php.zend.framework.mvc/2007-08/msg00158.html
http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-access-request-object-customizing-layout-view-3349.html
You can pass it in from a controller: $this->view->page = $this->_getParam('page');.
Footnote: I agree with #alexn.
i am using Zend Framework v1.11 and i am doing like this
In Controller
$this->view->request = $this->_request;
then in View you can access any Request param like this
<h3><?= $this->request->fullname ?></h3>