Laravel: Injecting Form request object to a controller method - laravel

In my routes file
$this->post('/form', [FormFacade::class, 'createMethod]);
In my FormFacade class:
............
...........................
public function createMethod(Request $request)
{
// do some stuff
// want to call controller's store method from here
}
In my FormController class
......................
public function store(FormRequest $request)
{
//am expecting validated data here onwards .....
//code to store validated data in the database
}
So, I need to call FormController:: create() method from my FormFacade. For that .... I need to inject the FormRequest object in the store method. How can i do that ? Any ideas is much appreciated.

You can create Requests classes.
https://laravel.com/docs/9.x/validation#creating-form-requests
Those classes extend Request class & Request class extends FormRequest so it's the same thing & you will follow Laravel Standarts

Related

Laravel dependency injection without relying on the controller

I'm trying to figure out how to inject a dependency into a class in Laravel.
My structure:
SimpleController extends BaseController
{
public function example(SimpleModel $model, SimpleValidationRequest $request)
{
$result = $model->doStuff()
return $this->makeResponse($result);
}
}
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
$service->doComplexLogic($this);
}
}
I have registered the ComplexService in my own service provider:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(ComplexService::class);
}
}
I want to inject the service straight into the simpleModel's doStuff method, without having to inject it into the controller and then into the model. We're busy moving a monolithic application to Laravel and have service classes that contain all the complex business logic. Much of the logic is shared between different classes, so a controller method might call a model that calls a service that ends up making 4 or 5 calls to other services, and I want to be able to inject another service into any method that needs it without having to send it down from the controller all the way through to the bottom method that might need it.
Is there a way to do this? I have been looking online but everything I've found has required me to inject the service into the controller and then sending it through the application from there, which I want to avoid.
You can simply call the singleton inside the method via the app() helper
SimpleModel extends Model
{
public function doStuff(ComplexService $service)
{
app()->singleton(ComplexService::class)->doComplexLogic($this);
}
}
or recover the singleton by injecting it in the model via it's __construct() method.

Pass Customer FormRequest to GET in Laravel

I'm curious is that possible to pass (as type-hinted) of custom class that extends FormRequest to be passed in to action within GET request ?
for example:
at routes/api.php
Route::get('/schema', '\App\Http\Controllers\TestController#getSchema');
then I have App\Http\Requests\SchemaRequest.php
and at controller, I want get this request from route within GET method.
class TestController extends Controller {
public function getSchema(\App\Http\Requests\SchemaRequest $request) {
// do other stuff here
}
}
I've tried to look deeper and doing some hack but nothing success yet?
Is that possible?
Any input would be appreciated, and thanks for reading
How about this workaround?
Route::get('/schema', 'TestController#getSchema',namespace =>'App\Http\Controller');
class TestController extends Controller {
public function getSchema(Request $request) {
// do other stuff here
}
}

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

What does the make() method do in Laravel?

In the Laravel documentation, I found the following - https://laravel.com/docs/5.4/container#the-make-method
but I am still confused as to what exactly the make() method does. I know the create() method uses the make() method and then persists them into the database, so does make() methods just temporarily save it in php tinker or something? Sorry, I'm Laravel noob. I'm trying to figure out these functions. Thank you! :)
The make method will return an instance of the class or interface you request.
Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class.
E.g.
$app->make('App\Services\MyService'); // new \App\Services\MyService.
One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.
E.g. an instance of the Mailer class would be automatically injected here.
namespace App\Services;
use \Illuminate\Mail\Mailer;
class MyService
{
public function __construct(Mailer $mailer) {
$this->mailer = new Mailer;
}
}
I discovered recently that when you use make (), you are installing the class and you can access the methods of that class or model, this is a useful for the Test and validate that you are getting what you want Example:
User model
class User extends Authenticatable
{
public function getRouteKeyName ()
     {
         return 'name';
     }
}
Test user
class UserTest extends TestCase
{
public function route_key_name_is_set_to_name ()
     {
$ user = factory (User :: class) -> make ();
$ this-> assertEquals ('name', $ user-> getRouteKeyName ());
// When you access the getRouteKeyName method you get the return, that is 'name'
}
}
On the other hand if you use "create" that would give an error because you are creating a user

Accessing function within another controller in Laravel 4

I have a function within an AdminController for sending emails. I want to access this within another controller. Can anyone advise how I would modify this to work?
OrdersController
public function postOrder()
{
$order = New Order;
...
$order->save();
// email order (call function in other controller)
$this->emailOrder($order);
}
AdminController
public function emailOrder($order)
{
//email processing goes here
}
You would strip it out, either into an abstract controller, that your controllers inherit from:
class AdminController extends MyController
Or to a service that you can call from your controller:
Mail::sendOrder($order)

Resources