I'm facing a problem in my code and, I think, the easier solution seems to edit an Illuminate Class of the Laravel Framework.
So I opened the file I wanted to edit (public/laravel/framework/src/Illuminate/Http/Response.php) and added a method headers which returns all the response headers :
public function headers()
{
return $this->headers;
}
But, this edit doesn't seem to be applied because, when I want to use:
Route::filter('cache.put', function($route, $request, $response){
$headers = $response->headers();
});
the Call to undefined method error is thrown.
My question is : how can I edit a Laravel Class ?
You should not be editing code that belongs to Laravel framework directly. You'll have trouble with updating the framework later on. Even if you managed to make it work the way you're trying, it has the potential to break your whole project since Laravel will still be using it's own Response class, while you try to use your own.
You need to check out the docs about Facades here to do it the Laravel way: http://laravel.com/docs/facades
In short, since the Response class you will be calling is a Facade, you need to replace Laravel's Response facade with one of your own, with your custom facade pointing to your own response class. Something like this:
MyResponseFacade.php:
<?php namespace MyApp;
class MyResponseFacade extends \Illuminate\Support\Facades\Response
{
public static function make($content = '', $status = 200, array $headers = array())
{
return new MyResponse($content, $status, $headers);
}
}
MyResponse.php
<?php namespace MyApp
class MyResponse extends \Illuminate\Http\Response
{
public function headers()
{
return $this->headers;
}
}
Then in your app.php, replace:
'Response' => 'Illuminate\Support\Facades\Response',
with this:
'Response' => 'MyApp\MyResponseFacade',
And you are done! The whole Laravel app should be returning you Response that is your own class, and you can use the rest of your code with the same name Response.
Make sure you configure the autoload properly, and do php artisan dump-autoload so Laravel can see your new files though.
As a side note, it seems like you are using Laravel by cloning the framework project. Check the answers in here for a better way: Should I download Laravel for every project?
Related
I'm working on a large Laravel app, currently on v8.45.1 which has never had tests, so I'm working to get it to a point where we can start writing unit & feature tests.
I'm hitting an issue where the two request classes (App\Core\Request and App\Core\FormRequest) both use a trait RequestTrait which holds a set of utility methods.
This obviously works fine in local/staging/production, but when I run the test suite it complains that none of the methods provided by the trait exist:
Method Illuminate\Http\Request::isFromTrustedSource does not exist.
They are being called in various places as Request::isFromTrustedSource() or request()->isFromTrustedSource().
I can imagine that when running the app in the test environment, there may be differences to the request. Is it using a different class, or does the trait not apply for some reason?
I think, I found your problem - App\Core\Request extends Illuminate\Http\Request and in index.php you use App\Core\Request
The problem is in Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::call()
When you use $this->get(...) in test suite - this method bootstrap app with standard request - not with your App\Core\Request
You can override this method in base tests/TestCase.php and pass your own request.
Unfortunately, it has no contract, than you cannot work with this through $this->app->bind()
Something like this:
class TestCase extends BaseTestCase
{
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
//other code
$response = $kernel->handle(
$request = \App\Core\Request::createFromBase($symfonyRequest)
);
//other code
}
}
I am developing app in laravel (REST server), using Basic Auth. Using Postman, all GET requests I have implemented seem to work, but unfortunately POST requests not.
routes.php:
Route::post('my/action', 'MyController#postMyAction');
My Controller:
public function __construct()
{
$this->middleware('auth.basic.once');
}
public function postMyAction($request)
{
// some logic here
}
The problem is, that this way, after setting credentials and some params in Postman, following exception appears:
Missing argument 1 for
App\Http\Controllers\MyController::postMyAction()
Does anybody knows how to put request into post-processing function defined in routes?
Thanks in advance.
Laravel provides dependency injection for controller methods, however you need to typehint exactly what you want so Laravel knows what to inject:
public function postMyAction(\Illuminate\Http\Request $request)
{
// Now $request is available
Now Laravel knows you want an instance of Illuminate\Http\Request and it will give it to you.
Of course you can also stick use Illuminate\Http\Request; at the top of your controller then just typehint Request $request as the argument.
I need to use a custom Implementation of UrlGenerator. So how can I change the default binding of laravel, that is implemented somewhere deep in the core as
'url' => ['Illuminate\Routing\UrlGenerator', 'Illuminate\Contracts\Routing\UrlGenerator'],
against my own implementation?
Furthermore I am not shure. I assume this line above does actually two things. it will store the bindinung under the key "url" and it will also do the mapping of the Interface to the class. So I actually need to override both! How to do that? Furthemore how to find out if this must be bound as "shared"(singleton) or "new instance every time"?
Thanks very much!
Take a look at the Service Container guide http://laravel.com/docs/5.1/container
In this specific case I think all you need to do is to tell the app to replace the alias that already exists.
To do that I would recommend creating a ServiceProvider, registering int the config/app.php file and inside that one in the register method put something like:
$this->app->bind('Illuminate\Routing\UrlGenerator', 'yourownclasshere');
Let us know if it works.
Update: I removed the option that didn't work and left only the one that worked.
I did what Nestor said in his answer, but it didn't quite work for me. So this is what I did to make it work.
Inside my service provider in method register I first tried this:
$this->app->bind('url', MyCustomProvider::class);
This did register my URL provider instead of the default one. The problem was that now my provider didn't have any access to routes. I checked the Laravel code for \Illuminate\Routing\RoutingServiceProvider because it has a method registerUrlGenerator for registering the URL provider. This method did a direct instantiation of the Laravel URL generator Illuminate\Routing\UrlGenerator and giving proper parameters in the constructor.
So, I did the same in my service provider. Instead of doing $this->app->bind I did $this->app->singleton('url', function ($app) { ... }) and provided basically the same code in the closure function as in RoutingServiceProvider::registerUrlGenerator but created the instance of my URL generator. This then worked properly, and my generator is now called every time. The final code was this:
// the code is copied from the \Illuminate\Routing\RoutingServiceProvider::registerUrlGenerator() method
$this->app->singleton('url', function ($app) {
/** #var \Illuminate\Foundation\Application $app */
$routes = $app['router']->getRoutes();
$app->instance('routes', $routes);
// *** THIS IS THE MAIN DIFFERENCE ***
$url = new \My\Specific\UrlGenerator(
$routes,
$app->rebinding(
'request',
static function ($app, $request) {
$app['url']->setRequest($request);
}
),
$app['config']['app.asset_url']
);
$url->setSessionResolver(function () {
return $this->app['session'] ?? null;
});
$url->setKeyResolver(function () {
return $this->app->make('config')->get('app.key');
});
$app->rebinding('routes', static function ($app, $routes) {
$app['url']->setRoutes($routes);
});
return $url;
});
I hate copying the code, so it seems to me that the problem is in the base implementation. It should take the correct contract for URL generator instead of making direct instantiation of a base class.
I tried the Kosta's approach but it didn't fully work for me because it somehow created an endless recursion loop in the framework. Nonetheless, I ended up with this code:
namespace App\Providers;
use App\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
class UrlGeneratorServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton("url", function($app) {
$routes = $app['router']->getRoutes();
return new UrlGenerator( // this is actually my class due to the namespace above
$routes, $app->rebinding(
'request', $this->requestRebinder()
), $app['config']['app.asset_url']
);
});
}
protected function requestRebinder()
{
return function ($app, $request) {
$app['url']->setRequest($request);
};
}
}
And of course, registered the above provider in config/app.php under 'providers'
So I am not really sure how to go about this I have tried a few things and I will list one below however what I am trying to do is store information sent in a http request in a PHP variable.
Here is a view from Chrome Postman of me sending the request I want ot send. Note "pubapi" is a "header".
I have been messing around with Lumen requests as you can see documented here ( http://lumen.laravel.com/docs/requests ) and have tried using the following below to possibly display them but its not working obviously.
echo Request::all();
I am putting this in my controller and I have ...
use Illuminate\Http\Request;
in my controller.
So how could I say store the header I am sending "pubapi" into a php variable in my controller?
EDIT
Not sure if this will help, however looking at the Laravel frameworks docs I see this http://laravel.com/api/5.0/Illuminate/Http/Request.html#method_header trying this throws the same error in my code. So for example I tried the following and reached the same error.
echo Request::header('pubapi');
You misunderstand the Laravel request object on two levels.
First, the error you are getting is because you were referencing the object instead of the Facade. Facades have a way of forwarding static method calls to non-static methods.
Second, you are sending the value as a header but are trying to access the request parameters. This will never give you what you want.
Here is a simple way to see an example of what you want by creating a test route like so:
Route::match(['get','post'], '/test', function (Illuminate\Http\Request $request) {
dd($request->headers->all());
});
Post to this route and you will see your headers, one of which will be pubapi. Pay attention that the route method definition matches how you are submitting the request (ie GET or POST).
Let's apply this to the controller, ArticleController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function index(Request $request)
{
$pubapi = $request->header('pubapi'); // string
$headers = $request->headers->all(); // array
/*
$pubapi === $headers['pubapi']
*/
}
}
Try to change the Illuminate\Http\Request to Request.
- use Illuminate\Http\Request;
+ use Request;
Using
echo app('request')->header('pubapi');
Instead of
echo Request::header('pubapi');
Seemed to work perfect. Could someone provide additional explanation to why this worked and my original method didn't?
Actually you are calling it statically, that's why it is not getting appropriate Request class and throwing error, can do as follows
use Illuminate\Http\Request;
//inside your controller
class YourClass extends Controller{
public function yourFunction(Request $request){
//for getting all the request
dd($request->all());
//for getting header content
dd($request->header('pubapi'));
}
}
You can do it by request()->header('pubapi') also.
I prefer to use request() helper function than Request class its self. Because it can be used without use/import any class in the controller.
I would like to extend Laravels Router class (Illuminate\Routing\Router) to add a method I need a lot in my application.
But sadly I can't get this to work. I already extended other classes successfully so I really have no idea where my wrong thinking comes from.
Anyway, right into the code:
<?php
namespace MyApp\Extensions;
use Illuminate\Routing\Router as IlluminateRouter;
class Router extends IlluminateRouter
{
public function test()
{
$route = $this->getCurrentRoute();
return $route->getParameter('test');
}
}
So as you see I want to get the parameter set by {test} in routes.php with a simple call like:
Router::test();
Not sure how to go on now. Tried to bind it to the IOC-Container within my ServiceProvider in register() and boot() but I got no luck.
Whatever I try I get either a constructor error or something else.
All solutions I found are too old and the API has changed since then.
Please help me!
edit:
I already tried binding my own Router within register() and boot() (as said above) but it doesn't work.
Here is my code:
<?php
namespace MyApp;
use Illuminate\Support\ServiceProvider;
use MyApp\Extensions\Router;
class MyAppServiceProvider extends ServiceProvider {
public function register()
{
$this->app['router'] = $this->app->share(function($app)
{
return new Router(new Illuminate\Events\Dispatcher);
}
// Other bindings ...
}
}
When I try to use my Router now I have the problem that it needs an Dispatcher.
So I have to do:
$router = new Router(new Illuminate\Events\Dispatcher); // Else I get an exception :(
Also it simply does nothing, if I call:
$router->test();
:(
And if I call
dd($router->test());
I get NULL
Look at: app/config/app.php and in the aliases array. You will see Route is an alias for the illuminate router via a facade class.
If you look at the facade class in Support/Facades/Route.php of illuminate source, you will see that it uses $app['router'].
Unlike a lot of service providers in laravel, the router is hard coded and cannot be swapped out without a lot of work rewiring laravel or editing the vendor source (both are not a good idea). You can see its hardcoded by going to Illuminate / Foundation / Application.php and searching for RoutingServiceProvider.
However, there's no reason i can think of that would stop you overriding the router class in a service provider. So if you create a service provider for your custom router, which binds to $app['router'], that should replace the default router with your own router.
I wouldn't expect any issues to arise from this method, as the providers should be loaded before any routing is done. So overriding the router, should happen before laravel starts to use the router class, but i've not this before, so be prepared for a bit of debugging if it doesn't work straight away.
So I was asking in the official Laravel IRC and it seems like you simply can't extend Router in 4.1 anymore. At least that's all I got as a response in a pretty long dialogue.
It worked in Laravel 4.0, but now it doesn't. Oh well, maybe it will work in 4.2 again.
Other packages suffer from this as well: https://github.com/jasonlewis/enhanced-router/issues/16
Anyway, personally I'll stick with my extended Request then. It's not that much of a difference, just that Router would've been more dynamic and better fitting.
I'm using Laravel 4.2, and the router is really hard coded into the Application, but I extended it this way:
Edit bootstrap/start.php, change Illuminate\Foundation\Application for YourNamespace\Application.
Create a class named YourNamespace\Application and extend \Illuminate\Foundation\Application.
class Application extends \Illuminate\Foundation\Application {
/**
* Register the routing service provider.
*
* #return void
*/
protected function registerRoutingProvider()
{
$this->register(new RoutingServiceProvider($this));
}
}
Create a class named YourNamespace\RoutingServiceProvider and extend \Illuminate\Routing\RoutingServiceProvider.
class RoutingServiceProvider extends \Illuminate\Routing\RoutingServiceProvider {
protected function registerRouter()
{
$this->app['router'] = $this->app->share(function($app)
{
$router = new Router($app['events'], $app);
// If the current application environment is "testing", we will disable the
// routing filters, since they can be tested independently of the routes
// and just get in the way of our typical controller testing concerns.
if ($app['env'] == 'testing')
{
$router->disableFilters();
}
return $router;
});
}
}
Finally, create YourNamespace\Router extending \Illuminate\Routing\Router and you're done.
NOTE: Although you're not changing the name of the class, like Router and RoutingServiceProvider, it will work because of the namespace resolution that will point it to YourNamespace\Router and so on.