Getting request parameters on Slim - php

I'm trying to get request parameter names and values dynamically, but the array is always empty. This is the get route:
$app->get('/get/profile/:id_user', function ($id_user) use ($app) {
print_r($app->request()->params());
});
And this is how im calling it from the browser:
http://localhost/get/profile/9492
This should return an array with id_user => 9492but it comes empty.
Any ideas why?

Notice: Please read update notes before trying out this code. The update note is my first comment in this answer.
Couldn't get to test it but please try the following:
$app->get('/get/profile/:id_user', function ($id_user) use ($app) {
$req = $app->request();
print_r($req->params());
});
Reference documentation: http://docs.slimframework.com/#Request-Method
Update: Okay after some digging figured the following, the params() method requires a parameter. If called without a parameter a Notice is raised. Checking the source revealed that this function called without a parameter returns null. See Http/Request.php line 199. Also for some reason currying does not seem to work either to retrieve parameters so you have to use the function parameter $id_user which has the expected value.

You can use following:
$app->get("/test.:format/:name",function() use ($app){
$router = $app->router();
print_r($router->getCurrentRoute()->getParams());
});

Also is a configuration problem。
try
try_files $uri $uri/ /index.php?$query_string;

Related

404 not found : laravel api with parameter

Call to Laravel API with parameter gets an error of 404: page not found, But while removing the parameter It works fine.
API.php have the following code
Route::get('Parties/{aToken}',"CustomerController#apiParties");
The controller has the following Code
function apiParties(request $request,$token){
$parties = DB::table('parties')
->Where("status","1")
->get()
->take(20);
return json_encode($parties);
}
Tried too many things but not working. I'm working on the server, not in localhost so don't have a terminal.
Change this
->get()->take(20);
to
->take(20)->get();
more fluently :
return DB::table('parties')
->Where("status","1")
->take(20)
->toJson();
Only use Request when you need it, i see that you not really use it on this scope of code. And make sure you already import DB Facades correctly :
use Illuminate\Support\Facades\DB;
If you want to make the parameter optional then add ? before the close brace.
Second thing is that you need to use Request $request starting with capital letter.
Always use small letters in the URLs and for the parameters.
Also, the parameter in the controller method should be Request instead of request.

Get extension key inside of post processor

How can I fetch the extension key in my post processor?
I tried it like this like suggested here
public function returnExtkey() {
return t3lib_div::camelCaseToLowerCaseUnderscored($this->extensionName);
}
However, I get:
Fatal error: Class 'MyCompany\MyExtension\PostProcess\t3lib_div' not found
I also tried to call it without the function camelCaseToLowerCaseUnderscored:
echo "EXTNAME = '".$this->extensionName."'";
But I get an empty string as result.
How can I solve this?
I suggest to ask the request object for the extension key:
$extName = $this->request->getControllerExtensionKey()
by the way: t3lib_div was replaced by \TYPO3\CMS\Core\Utility\GeneralUtility

Passing "\" and "/" in variable via Laravel Route

First off, apologies if this is a bad question/practice. I'm very new to Laravel, so I'm still getting to grips with it.
I'm attempting to pass a variable that contains forward slashes (/) and backwards slashes () in a Laravel 5 route and I'm having some difficulties.
I'm using the following: api.dev/api/v1/service/DfDte\/uM5fy582WtmkFLJg==.
Attempt 1:
My first attempt used the following code and naturally resulted in a 404.
Route:
Route::group(array('prefix' => 'api/v1'), function() {
Route::resource('service', 'ServiceController');
});
Controller:
public function show($service) {
return $service;
}
Result:
404
Attempt 2:
I did a bit of searching on StackOverflow and ended up using the following code, which almost works, however, it appears to be converting \ to /.
Route:
Route::group(array('prefix' => 'api/v1'), function() {
Route::get('service/{slashData}', 'ServiceController#getData')
->where('slashData', '(.*)');
});
Controller:
public function getData($slashData = null) {
if($slashData) {
return $slashData;
}
}
Result:
DfDte//uM5fy582WtmkFLJg==
As you can see, it's passing the var but appears to be converting the \ to /.
I'm attempting to create an API and unfortunately the variable I'm passing is out of my control (e.g. I can't simply not use \ or /).
Does anyone have any advice or could point me in the right direction?
Thanks.
As you can see from the comments on the original question, the variable I was trying to pass in the URL was the result of a prior API call which I was using json_encode on. json_encode will automatically try and escape forward slashes, hence the additional \ being added to the variable. I added a JSON_UNESCAPED_SLASHES flag to the original call and voila, everything is working as expected.
You should not do that. Laravel will think it is a new parameter, because of the "/". Instead, use htmlentitites and html_entity_decode in your parameter, or use POST.

Laravel - cannot get url GET parameters

I am having an issue trying to make a GET request to a route and process the parameters passed in via the URL. Here are two routes I created in routes.php:
$router->get('/', function() {
$test = \Input::get('id');
dd($test);
});
$router->get('test', function() {
$test = \Input::get('id');
dd($test);
});
When I go to the first URL/route ('/') and pass in some data, 123 prints to the screen:
http://domain.com/dev/?id=123
When I go to the second ('test') NULL prints to the screen (I've tried '/test' in the routes.php file as well).
http://domain.com/dev/test?id=123
A few things to note here:
This installation of Laravel is in a subfolder.
We are using Laravel 5.
Any idea why one would work and the other would not?
First thing - Laravel 5 is still under active development so you cannot rely on it too much at this moment.
Second thing is caching and routes. Are you sure you are running code from this routes?
Change this code into:
$router->get('/', function() {
$test = \Input::get('id');
var_dump($test);
echo "/ route";
});
$router->get('test', function() {
$test = \Input::get('id');
var_dump($test);
echo "test route";
});
to make sure messages also appear. This is because if you have annotations with the same verbs and urls they will be used and not the routes you showed here and you may dump something else. I've checked it in fresh Laravel 5 install and for me it works fine. In both cases I have id value displayed
You can use
Request::path()
to retrieve the Request URI or you can use
Request::url()
to get the current Request URL.You can check the details from Laravel 5 Documentation in here : http://laravel.com/docs/5.0/requests#other-request-information and when you did these process you can get the GET parameters and use with this function :
function getRequestGET($url){
$parts = parse_url($url);
parse_str($parts['query'], $query);
return $query;
}
For this function thanks to #ruel with this answer : https://stackoverflow.com/a/11480852/2246294
Try this:
Route::get('/{urlParameter}', function($urlParameter)
{
echo $urlParameter;
});
Go to the URL/route ('/ArtisanBay'):
Hope this is helpful.

In Kohana/PHP, how can send execution to a new controller/action?

In PHP/Kohana, I have controller action method which does some processing. When it is finished, I want to send it to another controller, e.g.:
public function action_import_csv()
{
Kohana_Import_Driver_Csv::process_files_from_csv_to_mysql($this->import_directory);
//url::redirect(Route::get('backend_application')->uri()); //undefined method URL::redirect()
//redirect(Route::get('backend_application')->uri(), null); //undefined function
}
According to this documentation at least the first redirect should work. I'm using Kohana 3.
How can I send execution from this controller action method to a new controller/action?
Addendum
For some reason, url::redirect is not available, here is the code completion I get for url:::
#bharath, I tried url::current() and got this error:
The problem is that you are looking at the Kohana 2 docs. Go to the kohana homepage and find the correct docs. Also, for some reason, everyone is giving you Kohana 2 answers even though you stated you're working with 3.
To redirect, do this from the context of a controller:
$this->request->redirect($something);
$something could be:
controller
controller/action
http://url.com
Here are the api docs for the redirect method (note that this uses url::site to parse the url; you may want to look at the source of that method too.
i am not very sure but i think you can simple use the redirect() function passing in the other controller you want to send to with any parameters
example
redirect(controllername/method)
Shouldn't that be :
url::redirect('controller/method');
And if it doesn't work, you probably had some output before calling the redirect (you'll probably get the "Headers already sent" error when that is the case).

Categories