How do I obtain the current url without params in Yii? - php

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

Related

How to URL encode a URI route parameter in Laravel

I have a URI I need to pass inside a route to a controller. How do I handle this, or more specifically, how can I pass a string that would typically need to be URL encoded first? Could this be Howhandledy, a regular expression constraint in the route?
String to pass
itm:n#_123445
Route
Route::get('getChildren/{uri}', 'ChildrenController#getChildren');
You can use URL facade to do that.
Full path is Illuminate\Support\Facades\URL or just use \URL since it is added in config/app.php file.
Usage:
URL::to('/getChildren', ['itm:n#_123445']));
Generated url:
http://domain.test/getChildren/itm%3An%23_123445
Handling
Route::get('getChildren/{url}', function ($url) {
dd($url); // itm:n#_123445
});
Hope this helps you
I would recommend updating your table schema to create a unique id for each uri in the DB, if it does not already have one. Instead of passing the full uri as a parameter, you would pass the id instead.
First off,
If you are using this specific format, the browser will understand the # as a reference to an anchor in the page - just like this example: https://laravel.com/docs/7.x/packages#views
and won't pass the number to the backend - as you mention you most likely will have to encode the URL before sending it
Now if you are sure that the backend can receive this format, I would do a preg_match in a middleware (if this format is recurrent) or directly in the controller to extract the numerical id.
preg_match('/itm:n#_(\d*)/', $uri , $matches);
$id = matches[0]

Access URL param from Input::get() with laravel

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.

Getting base URL in Yii 2

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/

How does one access the URL arguments in codeigniter if one was to follow REST architecture?

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 .

Zend Framework - need to access a GET parameter from a view

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>

Categories