I am using restangular to post a request to users/login
my laravel route is like so
Route::post('users/login', array('as' => 'login', function () {
$input = Input::all();
return Response::json($input);
}));
the data in the post header is formatted like so
{"input":["username":"un","password":"pw","remember":false]}
this dosn't work either
{"username":"un","password":"pw","remember":false}
this route is returning an empty array []. I am guessing my input is formatted incorrectly as from the laravel docs
Note: Some JavaScript libraries such as Backbone may send input to the
application as JSON. You may access this data via Input::get like
normal.
edit: it is working with this input ie. no quotes
{username:un,password:pw,remember:false]}
This is what you're looking for:
$input = Request::json()->all();
You can test it by returning the array directly from your route:
Route::post('users/login', array('as' => 'login', function ()
{
$input = Request::json()->all();
return $input;
}));
Related
I have been trying to get cakephp to suggest input from data that is from my tables like autocomplete. I've done some reading about how some other people have done this but still can't figure it out. Currently it seems that every time my controller is waiting for an ajax request and it is always false. No errors come up from the console some i'm not sure what i'm doing wrong. I tried removing the if ($this->request->is('ajax')) statement but then I get a error about it cannot emit headers.
Here is my search function in InvoicesController which I have taken code from someone else example but failed to implement it.
public function search()
{
if ($this->request->is('ajax')) {
$this->autoRender = false;
pr('b');
$name = $this->request->query['term'];
$results = $this->Invoices->find('all', [
'conditions' => [ 'OR' => [
'id LIKE' => $id . '%',
]]
]);
$resultsArr = [];
foreach ($results as $result) {
$resultsArr[] =['label' => $result['full_name'], 'value' => $result['id']];
}
echo json_encode($resultsArr);
}
}
And here is my search.ctp
<?php use Cake\Routing\Router; ?>
<?php echo $this->Form->input('id', ['type' => 'text']);?>
<script>
jQuery('#id').autocomplete({
source:'<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>',
minLength: 1
});
</script>
This is my invoice table and the ids are what I want to be suggested from what users type in.
I may not be seeing your exact problem but let me point out a few things I see that might help this issue.
Remove this line. It is not necessary
$this->autoRender = false;
Instead you should be doing this at the end. See using the RequestHandler
$this->set('resultsArr', $resultsArr);
// This line is what handles converting your array into json
// To get this to work you must load the request handler
$this->set('_serialize', 'resultsArr');
This will return the data without a root key
[
{"label":"Label Value"},
{"label":"Another Label Value"}
]
Or you can do it like this
$this->set('_serialize', ['resultsArr']);
This will return data like
{"resultArr":[
{"label":"Label Value"},
{"label":"Another Value"}
]}
Replace your finder query with this.
$resultArr = $this->Invoices->find('all')
->where(['id LIKE' => $id . '%'])
// If you want to remap your data use map
// All queries are collections
->map(function ($invoice) {
return ['label' => $invoice->full_name, 'id' => $invoice->id];
});
It seems to me you might want to review the new cakephp 3 orm. A lot of hard work went into writing these docs so that they could be easily read and relevant. I'm not one to push docs on people but it will save you hours of frustration.
Cakephp 3 ORM documentation
A few minor things I noticed that are also problems.
You never define $id.
You define $name but never use it.
pr is a debug statement and I am not sure why you have it.
Based on your comment, here is an update on ajax detection.
// By default the ajax detection is limited to the x-request-with header
// I didn't want to have to set that for every ajax request
// So I overrode that with the accepts header.
// Any request where Accept is application/json the system will assume it is an ajax request
$this->request->addDetector('ajax', function ($request) {
$acceptHeaders = explode(',', $request->env('HTTP_ACCEPT'));
return in_array('application/json', $acceptHeaders);
});
I'm working a Order List using Laravel 5 and I have this Reject button which is like this
When it is clicked it will confirm if the user really want to reject then if yes it will redirected to a specified route like so
Route::get('reject-order/{ordernum}', 'OrderController#rejectCustomerOrder');
Then in my rejectCustomerOrder
public function rejectCustomerOrder($ordernum)
{
var_dump(Input::get('reject_reason')); exit;
CustomerOrder::where('order_number', '=', $ordernum)->update(['status' => 2]);
$data = CustomerOrder::where('order_number', '=', $ordernum)->get();
$user = User::find($data[0]->created_by_id);
Mail::send('emails.message-rejected', ['user' => $user->name, 'order_num' => $ordernum], function ($m) use ($user) {
$m->to($user->email, '')->subject('Custtomer Order Rejected');
});
Problem is i can't get the reject_reason input field. It's always null. When you click the x button (reject button) it will ask if you really want to reject and you need to put the reject reason on the text field. How can I get that or pass it in the route?
Try to pass the following parameter in this method.
rejectCustomerOrder($ordernum,Request $request)
the access this way:
$request->reject_reason
Fixed now. :) Was able to so by making it
<a class="btn btn-danger btn-ok" id="btnReject">Reject</a>
And adding
$("#reject_reason").keyup(function() {
var oldDataHref = $("#btnReject").attr('href');
var newDataHref = oldDataHref + $('#reject_reason').val();
$("#btnReject").attr('href', newDataHref);
});
If it's a "post" route you don't need the parameter in route definition. If it's a "any" or "get" type then it's ok to have it there.
So, if it's POST then you can get the value with Input::get('input_name') and if it's get you can simply get it trought the controller method parameter, in your case $ordernum.
I have some filters on my view and I want to get the parameters of my current URL and do something like edit any of my items in the page and go back with all the filters again after edit.
My example URL:
localhost:8000/equipamentos/filtro?filter_descricao=APARELHO+ULTRASSOM&filter_patrimonio=0
Then I choose any item to edit and go to:
localhost:8000/equipamentos/332/edit
After I change something I want to be redirected to the same URL with the filters in the beginning, like redirect and append filtro?filter_descricao=APARELHO+ULTRASSOM&filter_patrimonio=0
Thanks!
Use the Input facade:
// All
$data = Input::all();
// $_REQUEST['foo']
$data = Input::get('foo'); // null if foo doesn't exist
$data = Input::get('foo', 'bar'); // if foo doesn't exist, the value is bar
Then you can handle the redirection in the controller on in a filter.
I just drafted out the code and it is working.
Route::get('/query', function() {
return Redirect::route('result', Input::query());
});
Route::get('/result', [ 'as' => 'result', 'uses' => function() {
return Response::make(Input::all());
}]);
Either Input::all() or Input::query() should work to retrieve GET parameters.
I'm using Laravel 4.2.11
I have this defined in my routes.php file
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
And in my login.blade.php file, the form starts as this
{{ Form::open(array('route'=>'Loguearse'))}}
I dont know why when i submit the form takes the second route instead the first one, even though I am pointing to the first one.
There must be a way to go to the same url from two different forms, that is what I want.
If you have two routes with the exact same URI and same method:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
How can Laravel know the difference between them when something hit /gestionAdministrador?
It will always assume the first one.
The name you set 'as' => 'RegistrarAdministrador' will be used to create URLs based on that route name, only, when something (browser, curl...) hit the URL the only ways to differentiate them is by
1) URL
2) URL parameters (which is basically number 1 plus parameters)
3) Method (GET, POST)
So you could change them to something like:
Route::post('gestionAdministrador/loguearse', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador/registrar', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
EDIT 2
What you really need to understand is that the name you give to a route ('as' => 'name') will not be part of your url, so this is not something that Laravel can use to differentiate your two URls, this is for internal use only, to identify your routes during the creation of URLs. So, those instructions:
$loguearse = URL::route('Loguearse');
$registrar = URL::route('RegistrarAdministrador');
Would generate exactly the same URL:
http://yourserver.dev/gestionAdministrador
EDIT 1 - TO ANSWER A COMMENT
Redirecting in Laravel is easy, in your controller, after processing your form, in any of your methods you can just:
return Redirect::to('/');
or
return Redirect::route('home');
Having a route like this one:
Route::get('/', array('as' => 'home', 'uses' => 'HomeController#index'));
So, your controller would look like this:
class AdministradorController extends Controller {
public function RegistrarAdministrador()
{
...
return Redirect::route('home');
}
public function Login()
{
...
return Redirect::route('home');
}
}
Actually you have only one route in your route collection, because:
You have following routes declared:
Route::post('gestionAdministrador', array('as' => 'Loguearse', 'uses' => 'AdministradorController#Login'));
Route::post('gestionAdministrador', array('as' => 'RegistrarAdministrador', 'uses' => 'AdministradorController#RegistrarAdministrador'));
Both of these used post method and this is post method:
public function post($uri, $action)
{
return $this->addRoute('POST', $uri, $action);
}
It calls addRoute and here it is:
protected function addRoute($methods, $uri, $action)
{
return $this->routes->add($this->createRoute($methods, $uri, $action));
}
Here $this->routes->add means Illuminate\Routing\RouteCollection::add() and the add() method calls addToCollections() and it is as follows:
protected function addToCollections($route)
{
foreach ($route->methods() as $method)
{
$this->routes[$method][$route->domain().$route->getUri()] = $route;
}
$this->allRoutes[$method.$route->domain().$route->getUri()] = $route;
}
The $routes is an array (protected $routes = array();) and it's obvious that routes are grouped by methods (GET/POST etc) and in each method only one unique URL could be available because it's something like this:
$routes['post']['someUrl'] = 'a route';
$routes['post']['someUrl'] = 'a route';
So, in your case, the last one is replacing the first one and in this case you may use different methods to declare two routes using same URL so it would be in different array, something like this:
$routes['post']['someUrl'] = 'a route';
$routes['put']['someUrl'] = 'a route'; // Route::put(...)
There must be a way to go to the same url from two different forms
Yes, there is a way and it's simply that you have to use the same route as the action of your form and therefore, you don't need to declare it twice.
What you want to do is a bad idea, you shouldn't be logging in and registering from the same route. With that said what you are saying isn't really possible. Routing in Laravel is first come first served. Basically it checks the route until the URI matches one and then calls that method on the controller or executes the callback. Your routes have to be the other way in your routes file. This will be fixed by changing the url.
I am able to view and delete the data by passing ID in the URl in format of:
/apis/view/id.json
using:
public function view($id) {
$api = $this->Api->findById($id);
$this->set(array(
'api' => $api,
'_serialize' => array('api')
));
}
Similarly I want to implement add and edit, where I can pass data in Json format in the HTTP body and store/edit it in the database.
I couldn't follow the this solution:
CakePHP API PUT with JSON input
I couldn't understand how to use
$data = $this->request->input('json_decode');
to achieve it.
Add can simply be used as given in documentation by appending .json to it. The URL at which you post the data will become /apis.json. This will automatically access the add() method.
Assuming you pass json values email and password in such format: {"email":"abc#def.com","password":"123456"}
public function add(){
$data=$this->request->input('json_decode', true ); //$data stores the json
//input. Remember, if you do not add 'true', it will not store in array format.
$data = $this->Api->findByEmailAndPassword($data['email'],$data['password']);
//simple check to see if posted values match values in model "Api".
if($data) {$this->set(array(
'data' => $data,
'_serialize' => array('data')));}
else{ $this->set(array(
'data' => "sorry",
'_serialize' => array('data')));}
}// The last if else is to check if $data is true, ie. if value matches,
// it will send the input values back in JSON response. If the email pass
// is not found, it will return "Sorry" in json format only.
Hope that answers your question! Put is also very similar, except it will check if the data exists, if it doesn't it will create or else it will modify existing data. If you have any further doubts don't hesitate to ask :)
As explained in the linked documentation, CakeRequest::input() reads the raw input data, and optionally passes it through a decoding function.
So $this->request->input('json_decode') gives you the decoded JSON input, and in case it's formatting follows the Cake conventions, you can simply pass it to one of Model save methods.
Here's a very basic (untested) example:
public function add()
{
if($this->request->is('put'))
{
$data = $this->request->input('json_decode', true);
$api = $this->Api->save($data);
$validationErrors => $this->Api->validationErrors;
$this->set(array
(
'api' => $api,
'validationErrors' => $validationErrors,
'_serialize' => array('api', 'validationErrors')
));
}
}
This will try to save the data and return the save result as well as possible validation errors.
In case the formatting of the input data doesn't follow the Cake conventions, you'll have to transform it accordingly.