Cannot Extract Params from URL - api

Good Afternoon,
I have a external GET request hitting a controller action but i cannot retrieve the params.
the URL looks like:
https://yourway.local/strava/webstatusrep?hub.verify_token=STRAVA&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.mode=subscribe
i have tried using
$param1 = $request->getQueryParam('hub.challenge');
or
$param = ArrayHelper::getValue(Yii::$app->request->get(), 'hub.challenge');
But also in Xdebug i can see they are not sitting in the “queryparams” section.

Just replace . with _ in param name when calling Yii2 api:
$paramValue = Yii::$app->request->get("hub_challenge");

Related

Rails routing to external site from model column through controller

I'm having an issue in my controller. I'm making url objects with original_url short_url and sanitized_url. I can create and save the links just fine. The issue i'm having is when following the short link back to mysite.com/short_url it needs to go through the controller show and grab the sanitized url and redirect to that external site.
Can someone help me figure out what's wrong with this code?
I'm getting undefined method 'sanitized_url'
urls_controller.rb - show
short = params[:short_url]
#url = Url.where("short_url = ? ", short)
redirect_to #url.sanitized_url
My routes.
root to: 'urls#index'
get "/:short_url", to: "urls#show"
get "shortened/:short_url", to: "urls#shortened", as: :shortened
resources :urls
Thank you
undefined method 'sanitized_url'
where returns AR collection. You need to apply sanitized_url on an instance
Below should work
short = params[:short_url]
#url = Url.where("short_url = ? ", short).first
redirect_to #url.sanitized_url

F3: Rerouting to a dynamic URL

I am having some difficulties rerouting to a dynamic URL from within my controller.
in routes.ini
GET /admin/profiles/patient/#patientId/insert-report = Admin->createReport
in the controller Admin.php, in method createReport():
$patientId = $f3->get('PARAMS.patientId');
My attempt (in Admin.php):
$f3->reroute('admin/profiles/patient/' . echo (string)$patientId . '/insert-report');
Question: How to reroute to the same URL (where some error messages will be displayed) without
changing completely the routing, that is attaching patientId as a URL query parameter ?
Thanks, K.
The echo statement is not needed to concatenate strings:
$f3->reroute('admin/profiles/patient/' . $patientId . '/insert-report');
Here are 3 other ways to get the same result:
1) build the URL from the current pattern
(useful for rerouting to the same route with a different parameter)
// controller
$url=$f3->build($f3->PATTERN,['patientId'=>$patientId]);
$f3->reroute($url);
2) reroute to the same pattern, same parameters
(useful for rerouting from POST/PUT/DELETE to GET of the same URL)
// controller
$f3->reroute();
3) build the URL from a named route
(useful for rerouting to a different route)
;config file
GET #create_report: /admin/profiles/patient/#patientId/insert-report = Admin->createReport
// controller
$url=$f3->alias('create_report',['patientId'=>$patientId']);
$f3->reroute($url);
or shorthand syntax:
// controller
$f3->reroute(['create_report',['patientId'=>$patientId']]);

How to make seo url for Yii $_GET method using url manager?

I'm working on a site on local server. I have made a form to search country,state and city. After getting the results I see the URL formatted as URL
I want to make this URL as URL
So here I want to know about URL manager rules so I can make it as I want.
Simply add this rule in your url-manager
"site/searchme/<country>/<state>/<city>" => "site/searchme"
Now, you need to have an action with this signature:
public function actionSearchme($country, $state, $city)
You can access $country, $state, $city from url inside this action. For example if your url be like http://localhost/yii_1/site/searchme/UnitedStates/Washington/NewYork, $country will equal "UnitedStates" and so on.

Hiding parameters in createURL - YII Framework

I am trying to pass array of values as parameter to controller action in YII Framework,
My URL is like very hard to see with array values.
Calling Controller Action:
var jString = JSON.stringify(val);
window.open ('".$this->createUrl('campaign/reportdrill')."/id/'+jString,'_blank');
URL Formed :
http://sks14/viacrm/campaign/reportdrill/id/%5B%7B%22Campaign%22:193,%22Filter%22:651,%22crm_post_code_categ_id%22:%221%22,%22crm_campaign_post_code_id%22:%22296%22,%22todate%22:%2214-05-2014%22,%22fromdate%22:%2201-05-2014%22,%22agent%22:%22%22%7D%5D
How to hide this parameter from user or is anyother way to pass array of values to controller action ?
This is the only way to pass parameters via the GET method of URLs. If you want to 'hide' the URL, use an AJAX load instead.
var jString = JSON.stringify(val);
$('body').load('".$this->createUrl('campaign/reportdrill')."/id/'+jString);
However, AJAX load cannot apply to opening a new window. You will still need to use your URL for that purpose.

How can I access query string parameters for requests I've manually dispatched in Laravel 4?

I'm writing a simple API, and building a simple web application on top of this API.
Because I want to "consume my own API" directly, I first Googled and found this answer on StackOverflow which answers my initial question perfectly: Consuming my own Laravel API
Now, this works great, I'm able to access my API by doing something like:
$request = Request::create('/api/cars/'.$id, 'GET');
$instance = json_decode(Route::dispatch($request)->getContent());
This is great! But, my API also allows you to add an optional fields parameter to the GET query string to specify specific attributes that should be returned, such as this:
http://cars.com/api/cars/1?fields=id,color
Now the way I actually handle this in the API is something along the lines of this:
public function show(Car $car)
{
if(Input::has('fields'))
{
//Here I do some logic and basically return only fields requested
....
...
}
I would assume that I could do something similar as I did with the query string parameter-less approach before, something like this:
$request = Request::create('/api/cars/' . $id . '?fields=id,color', 'GET');
$instance = json_decode(Route::dispatch($request)->getContent());
BUT, it doesn't seem so. Long story short, after stepping through the code it seems that the Request object is correctly created (and it correctly pulls out the fields parameter and assigns id,color to it), and the Route seems to be dispatched OK, but within my API controller itself I do not know how to access the field parameter. Using Input::get('fields') (which is what I use for "normal" requests) returns nothing, and I'm fairly certain that's because the static Input is referencing or scoping to the initial request the came in, NOT the new request I dispatched "manually" from within the app itself.
So, my question is really how should I be doing this? Am I doing something wrong? Ideally I'd like to avoid doing anything ugly or special in my API controller, I'd like to be able to use Input::get for the internally dispatched requests and not have to make a second check , etc.
You are correct in that using Input is actually referencing the current request and not your newly created request. Your input will be available on the request instance itself that you instantiate with Request::create().
If you were using (as you should be) Illuminate\Http\Request to instantiate your request then you can use $request->input('key') or $request->query('key') to get parameters from the query string.
Now, the problem here is that you might not have your Illuminate\Http\Request instance available to you in the route. A solution here (so that you can continue using the Input facade) is to physically replace the input on the current request, then switch it back.
// Store the original input of the request and then replace the input with your request instances input.
$originalInput = Request::input();
Request::replace($request->input());
// Dispatch your request instance with the router.
$response = Route::dispatch($request);
// Replace the input again with the original request input.
Request::replace($originalInput);
This should work (in theory) and you should still be able to use your original request input before and after your internal API request is made.
I was also just facing this issue and thanks to Jason's great answers I was able to make it work.
Just wanted to add that I found out that the Route also needs to be replaced. Otherwise Route::currentRouteName() will return the dispatched route later in the script.
More details to this can be found on my blog post.
I also did some tests for the stacking issue and called internal API methods repeatedly from within each other with this approach. It worked out just fine! All requests and routes have been set correctly.
If you want to invoke an internal API and pass parameters via an array (instead of query string), you can do like this:
$request = Request::create("/api/cars", "GET", array(
"id" => $id,
"fields" => array("id","color")
));
$originalInput = Request::input();//backup original input
Request::replace($request->input());
$car = json_decode(Route::dispatch($request)->getContent());//invoke API
Request::replace($originalInput);//restore orginal input
Ref: Laravel : calling your own API