As mention in https://github.com/laravel/framework/issues/10791
If we want to get the Merge request from middleware in the Form request
We could add the below function in our Form request
public function all()
{
$this->merge( $this->request->all() );
return parent::all();
}
But when i tried to use that function in my CartRequest i get an error
(2/2) ReflectionException
Class App\Http\Requests\Product\CartRequest does not exist
in RouteSignatureParameters.php (line 25)
I dont know why i could not use that method
NB : im using laravel 5.5
Rigth now, i call 2 request in my controller to get my custom request
function addCart(Request $request,CartRequest $cartRequest,$item_id){
$global_id = $request->globalId //this id is coming from middleware
}
But i think that is the bad method.
Is there any other method to get our custom request in middleware?
Related
I'm pretty new to Laravel. I'm trying to create a form to post to a function in my controller. It works fine, exactly as expected, until I try to add in a custom request validation. As soon as I've added it, if I run the form again I get a MethodNotAllowed error. Can someone tell me what is going wrong?
edit** sorry I forgot to post the code.
The request I made is pretty simple. The authorize is set to return true.
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'campaign_name'=>'required',
'campaign_message'=>'required_without:campaign_link',
];
}
In the controller I'm calling the namespace for the request at the top and have the function using that request. The function works just fine when the regular laravel request is called rather than my custom one.
public function store(CreateCampaignRequest $request)
{
//do code here
}
My form is using this action to send the request.
{!! Form::open(['method'=>'POST',
'action'=>'OrganizationCampaignController#store'])!!}
and the route is set to this
Route::post('dashboard/campaigns/send','OrganizationCampaignController#store');
Like I said everything works when the normal Request is called, it only throws the method not allowed when I try using my custom request.
Thank you!
I'm currently re-writing an API with multiple endpoints. However, for legacy purposes it's required that there is a single endpoint which can be used to access all other endpoints. Which endpoint we should redirect to is based upon a custom action header send along with the request.
Example:
Input: Header -> Action A
Output: Redirect to route '/some/url' 'ControllerA#someAction'
Input: Header -> Action B
Output: Redirect to route '/some/other/url' 'ControllerB#someOtherAction'
Normally, I could use the redirect() method but then I lose the body of the POST method. All the endpoints I declared are POST methods.
Basically, the question is how can I properly redirect a POST to another route?
Also note I can't use:
App::call('App\Http\Controllers\PlanningController#addOrUpdate', ['request' => $request]);
Since my method uses a custom Request class to handle the validation. I get an exception telling the argument should be the type of my custom class and Illuminate\Http\Request is given.
I've actually found the answer to my problem. I've created a middleware which will re-create the request based upon the value found in the header.
Here's the handle function of the middleware (only tested on Laravel 5.2):
use Request;
use Route;
use Illuminate\Http\Response;
...
public function handle($request, Closure $next, $guard = null)
{
// Get the header value
$action = $request->header('action');
// Find the route by the action name
$route = Actions::getRouteByName(action); // This returns some route, i.e.: 'api/v1/some/url'
// Perform the action
$request = Request::create(route, 'POST', ['body' => $request->getContent()]);
$response = Route::dispatch($request);
return new Response($response->getContent(), $response->status(), ['Content-Type' => 'text/xml']); // the last param can be any headers you like
}
Please note that this might conflict on your project with other middleware. I've disabled other middleware and created a special routegroup for this. Since we're redirecting the call manually to another route the middleware on that route is called anyway. However, you can also implement this code inside a controller function then there are no conflicting middleware problems!
I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs
This is driving me crazy as I think I'm doing the right thing but its not working correctly.
I have a route with a middleware attached to it like below;
Route::get('post/{id}/{name}', 'BlogController#post')->name('blog-post')->middleware('blogGuard');
As you can see I've defined 2 route params
In my controller I have below;
public function post () {
return view('pages.blog.post');
}
With the middleware defined like this;
public function handle($request, Closure $next)
{
if (is_null($request->input('id')) ||
is_null($request->input('name'))) {
return redirect()->route('blog-home');
}
return $next($request);
}
Now if I click on a link like so; http://blog.example.co.uk/post/153/firstpost the middleware should not fire correct?
This is not the case. The middleware executes and I'm redirected. But if I remove the middleware then I'm able to access the page.
Any help appreciated.
If you are trying to access route parameters you probably want to explicitly get them from the route.
$request->route('id'); // pulls the $route->parameter('id');
$request->route('name'); // pulls the $route->parameter('name');
$request->id will check the request inputs before falling back to returning a route parameter.
$request->input('id') will only check the input sources for the request and not the route params.
If you use $request->id expecting to get the route param 'id', one could break your logic by passing id=anythinghere to the querystring or adding a 'id' var to a post request.
Try this
if (!$request->id ||
!$request->name)
I'm building an API for user and admin.
Got stuck at edit user profile routing.
on admin route i use Route::resource('user', 'UserController')
on user route i use Route::get('profile', 'UserController#show')
At the show method Laravel default has
public function show($id)
{
}
the different between them is on admin I can use /id but on user i check their token from middleware and merge the request to get their user_id so there is no need for the API to use profile/{id}.
The question is how can I use the same method but there is an argument to fill and the route still /profile?
One of my solution is :
public function show($id){
if ($request->has('user_id')):
$id = $request->query('user_id');
endif;
}
It working but when i read the code, it's really redundant always checking it and replace the id.
Just place the request object as a parameter in your controller and get the input from the request object when you use your user route.
Thanks