why i am getting 404 on passing different function name in url? - php

I don't know how restler behaves. when i am passing getuser/2 it works fine, but when i am passing /getalluser it gives
{
error: {
code: 404,
message: "Not Found"
}
}
But when i changed the function name getalluser to buyalluser it works fine.
Can anybody tell me whats the problem with my function names in restler framework?

As you have already found out
Restler is using get, post, put, delete as method prefixes to automatically map them to respective HTTP method/verb GET is the default HTTP method, so if you dont prefix a method with any of the above, it will be mapped to GET method.

Related

Response in laravel component

I have a component SeenRecentlyProduct and return code:
return response()->view('components.products.seen-recently-product', compact('seenRecentlyProducts'))
->withCookie($cookie);
I get an error:
Illegal offset type in isset or empty
If I write this code in the controller, it works well. If I remove response() from the component, the same works well. Help please resolve the issue with the response and cookie in the component.

findOrFail Laravel 8 method not found

I am working on Laravel 8 , I have tried to use findOrFail method in order to set redirection to 404 error not found instead of displaying error but it is showing this error method findOrFail not found,
this is the line of code in ProfilesController that is causing the error
$user = User::findOrFail($user);
this is the output of the error
enter image description here
Thanks in advance,
Look like your code works fine. The issue Php Strom not able to identify methods.
I always use query() method to get autocomplete model methods
$user=User::query()->findOrFail($user);
or you might need to install any extension which can able to identify laravel methods

Symfony: type error when using flash bag in controller

In order to get the flash bag of the session and add a flash message, in the controller I call:
$request->getSession()->getFlashBag()->addFlash(...);
(where $request is an instance of Request)
but I get the following IDE type error:
Method 'getFlashBag' not found in
null|\Symfony\Component\HttpFoundation\Session\SessionInterface
The problem is that $request->getSession() returns a SessionInterface, which does not contain the getFlashBag method.
That's why the IDE is complaining, even if the actual object returned by that method is an instance of the Session class which has the getFlashBag method.
When inside a controller, a quick solution can just be using:
$this->addFlash(...);
instead of:
$request->getSession()->getFlashBag()->addFlash(...);

json_encode only works with JSON_PARTIAL_OUTPUT_ON_ERROR for Model with custom toArray

I have an Eloquent Model, for which I created a custom toArray()-method, to include fields from a meta table (which I get by using the eloquent-meta plugin):
class User extends Model{
// ... Other stuff
public function toArray(){
return array_merge(parent::toArray(), $this->getAllMeta()->toArray());
}
}
When I now try to send this model as a JSON response using Response::json(...), I get:
UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(),
"boolean" given.
I have traced the error to the JsonResponse.setData($data)-method, in which the json_encode-call returns false. The json_last_error()-method returns JSON_ERROR_SYNTAX and the json_last_error_msg()-method returns Syntax error.
Using the debugger I stopped on the line below and evaluated the statement myself. As expected, it does not work, however if I call it like this, it works:
json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
This returns the complete, valid JSON that I expect, without any missing or NULL values.
What's even stranger is, if I stop in the toArray()-method and supply the merged array to json_encode, it works fine, even without partial.
Am I overlooking something obvious here??
The problem was with the eloquent-meta plugin I used. Here's the relevant part from my issue:
I traced the error back to the
Helpers.maybeDecode($value)-method:
The current implementation tries to parse the value with
json_decode($value) and checks, whether that worked, by checking the
json_last_error()-function. The problem is, that this doesn't reset
the last error.
When the Helpers.maybeDecode($value)-method is called, while
Laravel is encoding the model, and the value it tried to decode
was not an valid json (for example, a simple string), the error
code is set, causing the json_encode()-function to see it and return
null. The problem is with the global nature of the error-variable.
My proposed workaround for this is to reset the
json_last_error()-function after checking if decoing worked, and the
only way I have found to do this is by decoding something valid (even
if it's just an empty array).
Here is the Pull Request with the fix.

stopping page not found errors when controller does not exist

In production mode (minimal errors), when a controller is not found, Zend gives a 404 page not found error. There are a couple of controllers that I don't want this activated for. Even though they don't exist, I don't want the page not found error activated. Is it possible to somehow block that error and give an empty page. I'm guessing, if at all possible, it has to be done at the plugin level since no controller really exists to handle this.
One possible solution would be to check the request object in your errorAction for controller and/or action that threw the exceptions (for non-existing controllers and actions you could also get their names this way). Based on this you could customize the rest of errorAction. For example:
public function errorAction() {
$errors = $this->_getParam('error_handler');
$whatController = $errors->request->getControllerName();
if ('secretController' == $whatController) {
return $this->_redirect('blankErrorPage');
}
// usual rest of errorAction
}

Categories