Yii2 - Get an app URL without params - php

I have no idea how to get a full url to my app web folder in Yii2.
The following rules:
<?=Yii::$app->getUrlManager()->getBaseUrl();?><br>
<?=Yii::$app->homeUrl;?><br>
<?=Yii::$app->getHomeUrl();?><br>
<?=Yii::$app->request->url;?><br>
<?=Yii::$app->request->absoluteUrl;?><br>
<?=Yii::$app->request->baseUrl;?><br>
<?=Yii::$app->request->scriptUrl;?><br>
<?=Url::to();?><br>
<?=Url::to(['site/index']);?><br>
<?=Url::base();?><br>
<?=Url::home();?><br>
<?=Yii::$app->getUrlManager()->getBaseUrl();?><br>
returns:
/yiiapp/web
/yiiapp/web/
/yiiapp/web/
/yiiapp/web/en/reset-password-request
http://website.com/yiiapp/web/en/reset-password-request
/yiiapp/web
/yiiapp/web/index.php
/yiiapp/web/en/reset-password-request
/yiiapp/web/site/index
/yiiapp/web
/yiiapp/web/
/yiiapp/web
when I need to get the (absoluteUrl is the closest one here):
http://website.com/yiiapp/web
I could probably combine one of the results with some $_SERVER var… but is it a solution?

I realize this post is quite old but I want to answer it anyway.
To get a full URL to your app web folder in Yii2 you can try these three options:
Url::to('#web/', ''); returns //website.com/yiiapp/web/
Url::to('#web/', true); returns http://website.com/yiiapp/web/
Url::to('#web/', 'https'); returns https://website.com/yiiapp/web/

You can use Yii::$app->getUrlManager()->createAbsoluteUrl() method or yii\helpers\Url::toRoute() to generate absolute urls. yii\helpers\Url::to() also can be used look at the documentation. E.g. <?=Url::to(['site/index'], true);?> should output http://website.com/yiiapp/web/site/index. If you need to get root url to your app, try \yii\helpers\Url::to('/', true);

There are multiple ways to achieve this, but probably the most clean way to get base URL of your app is to use Url::base():
Url::base(true);
Most of methods in Url helper allows to you specify $scheme argument - you should use it if you want to create absolute URL (with domain).
The URI scheme to use in the returned base URL:
false (default): returning the base URL without host info.
true: returning an absolute base URL whose scheme is the same as that in yii\web\UrlManager::$hostInfo.
string: returning an absolute base URL with the specified scheme (either http, https or empty string for protocol-relative URL).

Related

Get current url path

How many examples I have not seen, all are essentially the same, and perform the same thing. Let's take a look at one of them.
Route::current()->uri()
We have url https://example.com/test and we get test, and in all examples the same
But how to make sure that we get not just test but with a slash /test?
You can get it with this piece of code:
request()->getPathInfo();
Laravel Illuminate/Http/Request extends Symfony Request class which contains getPathInfo method.
Docs: https://symfony.com/doc/current/components/http_foundation.html#identifying-a-request
Definition of that method you can find here.
You can get url in laravel :
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
You can try this:
request()->getPathInfo();
You can find the method definition here

How to use $routeProvider with $_GET Parameters

I'm new with the angularjs and I want your help. I'm trying to include the $routeProvider into my project in order to use templating system.
$routeProvider.when('/admin.php?page=all_transactions', {
templateUrl : 'pages/home.html',
controller : 'TransactionsController'
});
I saw that most of the examples i found the url of .when had the following format /route1/:param for urls like #/route1/12345
Because I'm using the angular in wordpress admin page I want the .when to work with $_GET parameters like the one I gave with the example code.
The depth of parameters I want it to be up to 3 and ignore any other parameters.
Does anyone know how I can do it?
is it enough for u to know, that the params are there? Or do u need explicit values. If the params are enough u could follow this example: URL Routing with Query Parameters
url: "/contacts?myParam"
// will match to url of "/contacts?myParam=value"
If you need to have more than one, separate them with an '&':
url: "/contacts?myParam1&myParam2"
// will match to url of "/contacts?myParam1=value1&myParam2=wowcool"
Hope this helps.
Edit: For accessing the values u can do the following: Accessing query parameter values
Also can get other arbitrary params in the query string form /view/1/2?other=12 with $routeParams.other – DavidC Aug 17 '14 at 21:04
OR:
While routing is indeed a good solution for application-level URL parsing, you may want to use the more low-level $location service, as injected in your own service or controller:
var paramValue = $location.search().myParam;
This simple syntax will work for http://example.com/path?myParam=someValue. However, only if you configured the $locationProvider in the html5 mode before:
$locationProvider.html5Mode(true);
Otherwise have a look at the http://example.com/#!/path?myParam=someValue "Hashbang" syntax which is a bit more complicated, but have the benefit of working on old browsers (non-html5 compatible) as well.

Using multiple nested _GET variables in a single URL

Setup:
Script that generates word images from multiple letter images
(autotext.php)
URL is formatted:
www.whatever.com/autotext.php?text=hello%20world
Script that alters images server-side to run filters or generate
smaller sizes (thumbnail.php)
URL is formatted:
www.whatever.com/thumbnail.php?src=whatever.png&h=XXX&w=XXX
Use-case:
I want to generate a smaller version of the autotext server-side. So my call would look something like:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
As you can see, I would like to treat a URL with _GET variables as a variable itself. No amount of playing with URI encoding has helped make this work.
I have access to the PHP for both scripts, and can make some simple alterations if that's the only solution. Any help or advice would be appreciated. I would not even rule out a Javascript frontend solution, though my preference is to utilize the two scripts I already have implemented.
You should be able to do this by urlencoding all the $_GET params into a variable then assigning that variable to another, like this (untested):
// Url generation
$url = www.whatever.com/thumbnail.php?src=(urlencode(http_build_query($_GET)));
Then you should be able to retrieve on other side:
$src = urldecode(explode('&', $_GET['src']));
I've seen this exact behavior when trapping where to redirect a user, after an action occurs.
---- Update ----
Your "use case" url was correct:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
.... except that you CANNOT have more than one ? within a "valid" url. So if you convert the 2nd ? to a &, you should then be able to access $_GET['text'] from the autotext.php script, then you can urldecode it to get the contents.

how to process php REST url resources

I have read many about REST api in php articles. but I still get quite confusing.
they basically rewrite the url to a index.php, which process the url and depends on the method, then send response
but which is the properly way to process the url? this looks doen't look correct...
get the uri and split it
I should know what to do with each portion, eg. for GET /usr/1 I should do something like:
if($myUri[0]=="usr")
getUser($myUri[1]);
if the request url is like GET www.domain.com/user/1
it would call getUser($id);
but what happen if you can also retrieve the user by name, or maybe e-mail? so the url can also be www.domain.com/user/john or www.domain.com/user/john#gmail.com
and each url should call different methods like getUsrByName($name) or getUsrByEmail($mail)
The proper way of handling this would be to have URLs like this:
domain.com/user/id/1 -> user::getById
domain.com/user/email/foo#bar.com -> user::getByEmail
domain.com/user/username/foo -> user::getByUsername
However, specifying multiple "parameters" is more like a search, I'd go against using resources for that, because a path should be absolute. Which means:
domain.com/user/name/Kossel/likes/StackOverflow
And:
domain.com/user/likes/StackOverflow/name/Kossel
Are not the same resource. Instead I'd do:
domain.com/user/?name=Kossel&likes=StackOverflow
This is what Stack Overflow uses:
stackoverflow.com/questions/tagged/php
stackoverflow.com/tags/php/new
stackoverflow.com/questions/tagged/mysql?sort=featured
To avoid long if/else statement, use variable function names. this allows you to use the url string to call the correct function.
http://php.net/manual/en/functions.variable-functions.php
Also, you may want to use classes/class methods instead of functions. this way you can set up an __autoload function, which will allow you to only load code that you are going to use each time the index.php is called.
MVC architecture usually breaks their urls into /class_name/class_method_name/arguments...

variable number of parameters from a url/paramname/valueparam in an array?

basicly I would like to read url params in an array so finding params don't depend on their place in url
I have a url for seach with controller/action/paramA/valueparamA/paramB/valueparamB
theses params are optional : I have direct url with search params inside
to read params from url we have to use action(valueparamA, valueparamB)
but for me it seems really rigid
I want to read parameters by their name, not by their place in url!
so I can have different urls like
urlA = controller/action?paramA=valueA
*(or controller/action/paramA/valueA)*
urlB controller/action?paramB=valueB
than I can use with the same action, like we do with a form with $_POST array (it and $_GET[} seems always empty when direct url params)
the best would be to have all parameters in an array[paramname=>paramvalue] like in a form
what I DON'T want is tu use differents actions for different parameters possibles! :)
the best I saw was to use juste on array like parameter :
controller/action/array[paramname=>paramvalue]
(passing arrays as url parameter)
but it seems to complicate something basic :
just read the normal url parameters like every framework knows :) with
url?nameparam=valueparam&...
hope there is a solution !
I begin tor eally like the light and quick of ci but sometimes (like for extending model) it seems a little "rigid" ;)
think in advance for any idea!
Yep the URI class provides this functionality in the form of
$this->uri->uri_to_assoc(n);
This returns an array containing all parameters (keys and values must be defined in the URL).
Full details can be found on the Codigniter Userguide:
http://codeigniter.com/user_guide/libraries/uri.html

Categories