PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] Get url parameter from Service Provider in Laravel

 March 07, 2022     laravel, laravel-5, php     No comments   

Issue

Normally in Laravel you get a request bag with all parameters in it. But in a service provider this is not the case. But we do have the request() object.

However, this does not contain the url parameters given in the resource routing.

So what I'm looking for is a way to retrieve the {id} from the route.

I have tried:

request()->route('id')
Route::get('id')
Request::get('id')

All of these return null


Solution

Both boot() and register() methods of any service provider are called before request is parsed and request parameters are known, that's why you can't access them.

Service providers are supposed to register services and shouldn't depend on the request context. If you want to use request parameters in your services, declare request service as a dependency of your service so that it's injected the moment your service gets instantiated.



Answered By - jedrzej.kurylo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,207,275

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing