404 not found : laravel api with parameter - php

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.

Related

what is the difference between return redirect('/') and Redirect::to('/')->send(); function in laravel

First code working. See Below
The second code not working. See Below
Anyone can help provide the documentation for this code Redirect::to('/')->send();
The ->send() method should only be used in places where you cannot return a response directly. And even then, it should be used as few times as possible.
Normally, a request will go through a list of middlewares, end up in a controller which will return a response and exit through another list of middlewares. This is the normal way of returning a response.
When you use the send() function, you interrupt the pipeline and send the response back immediately.
Now, the reason only send() works in your case is because you are redirecting from a constructor and a constructor cannot have a return value. Because of this, the second example will never work.
I would encourage you to use a middleware for this check instead. This way the check is reusable and you can return a redirect response without interrupting the pipeline.

How do you get the HTTP host with Laravel 5

I'm trying to get the hostname from an HTTP request using Laravel 5, including the subdomain (e.g., dev.site.com). I can't find anything about this in the docs, but I would think that should be pretty simple. Anyone know how to do this?
Good news! It turns out this is actually pretty easy, although Laravel's Request documentation is a bit lacking (the method I wanted is inherited from Symfony's Request class). If you're in a controller method, you can inject the request object, which has a getHttpHost method. This provides exactly what I was looking for:
public function anyMyRoute(Request $request) {
$host = $request->getHttpHost(); // returns dev.site.com
}
From anywhere else in your code, you can still access the request object using the request helper function, so this would look like:
$host = request()->getHttpHost(); // returns dev.site.com
If you want to include the http/https part of the URL, you can just use the getSchemeAndHttpHost method instead:
$host = $request->getSchemeAndHttpHost(); // returns https://dev.site.com
There two ways, so be careful:
<?php
$host = request()->getHttpHost(); // With port if there is. Eg: mydomain.com:81
$host = request()->getHost(); // Only hostname Eg: mydomain.com
laravel 5.6 and above
request()->getSchemeAndHttpHost()
Example of use in blade :
{{ request()->getSchemeAndHttpHost() }}
You can use request()->url();
Also you can dump the complete request()->headers();
And see if that data is useful for you.

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.

Phil Sturgeon’s REST API always returning: status:0, error:Unknown method

I am always getting the this error.
{"status":false,"error":"Unknown method."}
But all syntax are correct from my side. because everything is working fine on the browser but same URL integration on the devices gives the 'unknown method error'.
I am using this 'get' method. Sample URL
SITEURL/api/login/test?req_type=custom
Am I missing something while integrating? Perhaps a setting? I have just included the library and rest config file.
I think your problem is the name of controller is the same with the name of method try to make a test:
if the name of your controller is:
class Test extends REST_Controller{
//your method name is different from the name of controller class
public function testget_get(){
echo $this->response(array('test'=> 'test'), 200);
}
}
I have experienced this problem on hmvc structure.
You also need to check that from device which method your are getting means they are sending 'POST' or 'GET' so you can update your function name accordingly.
In my case I have done the function name as _get to the methods but from device methods of sending parameter is 'POST' which I am trying to access as 'GET'.
So please cross check this once.
When you create a method with the library, you need to append the type of request you are going to make to it.
So, if your method is test, and you are making a GET request to it, it needs to look like this:
function test_get(){
...
}
Same with POST requests
function test_post(){
...
}
Same with PUT, and DELETE as well.
NB This is only a guess since you didn't include any of your code for some reason.

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