Laravel where is "extend" method for "Validator" declared - laravel

I will give an analogy to explain what I want:
For example, if I want to read about the "behaviour" of the save() method in:
$model->save();
then I can go to Illuminate\Database\Eloquent\Model
to read what's inside
public function save() {....}
QUESTION: If I want to know the behaviour of extend() in
Validator::extend('','');
Where in the system laravel files should I go?
Thank you in advance!:)

Facades are a bit harder to find than other classes, since they only reference a service container binding. You'd have to find the class where this binding is registered to find the backing class.
The easiest way, for official facades, is to just check the Facades Documentation, there is a list of facades and their backing classes at the bottom:
Validator Illuminate\Validation\Factory validator
So the Validator facade resolves to the class Illuminate\Validation\Factory which is bound to the service container with the name validator. From here on, it should be easy to find the Illuminate\Validation\Factory class in your vendor directory.

Related

How does Dependency Injection work in this case?

In a Spring Boot Web Application layout, I have defined a Service Interface named ApplicationUserService. An implementation called ApplicationUserServiceImpl implements all the methods present in ApplicationUserService.
Now, I have a Controller called ApplicationUserController that calls all the methods of ApplicationUserServiceImpl under different #GetMapping annotations.
As suggested by my instructor, I have defined a Dependency Injection as follows:
public class ApplicationUserController {
private final ApplicationUserService applicationUserService; //This variable will work as an object now.
public ApplicationUserController(ApplicationUserService applicationUserService) {
this.applicationUserService = applicationUserService;
}
#GetMapping
//REST OF THE CODE
}
I am new to Spring Boot and I tried understanding Dependency Injection in plain English and I understood how it works. I understood that the basic idea is to separate the dependency from the usage. But I am totally confused about how this works in my case.
My Questions:
Here ApplicationUserService is an Interface and it's implementation has various methods defined. In the code above, applicationUserService now has access to every method from ApplicationUserServiceImpl. How did that happen?
I want to know how the Object creation works here.
Could you tell me the difference between not using DI and using DI in this case?
Answers
The interface layer is used for abstraction of the code it will be really helpfull when you want to provide different implementations for it. When you create a instance you are simply creating a ApplicationUserServiceImpl and assigning it into a reference variable of ApplicationUserService this concept is called upcasting. Even though you have created the object of child class/implementation class you can only access the property or methods defined in parent class/interface class. Please refer this for more info
https://stackoverflow.com/questions/62599259/purpose-of-service-interface-class-in-spring-boot#:~:text=There%20are%20various%20reasons%20why,you%20can%20create%20test%20stubs.
in this example when a applicationusercontroller object is created. The spring engine(or ioc container) will create a object of ApplicationUserServiceImpl (since there is a single implementation of the interface ) and returns to controller object as a constructor orgument which you assign to the refrence variable also refer the concept called IOC(Invertion of control)
as explained in the previous answer the spring will take care of object creation (object lifecycle)rather than you explsitly doing it. it will make the objects loosely coupled. In this the controll of creating the instances is with spring .
The non DI way of doing this is
private ApplicationUserService applicationUserService = new ApplicationUserServiceImpl()
Hope I have answered your questions
this analogy may make you understand better consider an example, wherein you have the ability to cook. According to the IoC principle(principal which is the basis of DI), you can invert the control, so instead of you cooking food, you can just directly order from outside, wherein you receive food at your doorstep. Thus the process of food delivered to you at your doorstep is called the Inversion of Control.
You do not have to cook yourself, instead, you can order the food and let a delivery executive, deliver the food for you. In this way, you do not have to take care of the additional responsibilities and just focus on the main work.
Now, that you know the principle behind Dependency Injection, let me take you through the types of Dependency Injection

How to extend Laravel core class MessageBags

I need to extend Illuminate\Support\MessageBag because of add some help methods but I cant bind my own class App\Support\MyMessageBag to laravel use my class (that extends App\Support\MyMessageBag) instead of original core class.
Do you have any ideas?

InitBinder for Spring Rest controller for #Valid

The way to use a Validator interface for Spring #RestController based is to create a validator custom class, implement Validator interface. In the controller class call InitBinder to register the validator class. Then when the REST req comes, the validator for that class is called. This still has a problem for me. Say I have a PersonValidator class that does one type of validation for POST and another type for PUT. Since both these handlers exist in the same REST controller class, how can I run different validations?
Say in the same rest controller class, i want to use PostPersonValidator for POST and PutPersonValidator for PUT. I do not know how to do it.
Just follow this article: http://howtodoinjava.com/2015/02/12/spring-mvc-custom-validator-example/
You basically have to create two separate validators - just as you said (one for POST, one for PUT). Then just call them inside your proper POST/PUT handling methods. Should be pretty straightforward if you follow the linked example.

call a method on a controller from another controller in mvc

I want to call a method on another controller. The problem is that in my project, all controllers are created using Windsor castle and Windsor castle resolve the dependencies. This means that I cannot instantiate a controller by using new as then it needs its dependents. How can ask MVC to instantiate a controller and then call a method on it (which returns an object)?
I'm going to tell you two things: 1) First, I'll show you how you'll most likely be able to accomplish what you want, and 2) Then I'll recomment you achieve your goal in another way :)
1: When using auto-wiring, the way to get stuff from the container is to declare the dependency by adding it as a constructor argument - e.g. (assuming the container knows how to resolve controllers by their concrete types):
public class HomeController
{
readonly AccountController accountController;
public HomeController(AccountController accountController)
{
this.accountController = accountController;
}
}
This will most likely allow you to do what you want with AccountController from withing HomeController. However, this is not very pretty.
2: Injecting one controller into another is probably not what you (really) want. I'm guessing that you really want to move whatever logic you have in your controller's action method into a dedicated service, e.g. DoSomethingInteresting which happens to implement IDoStuff, and then let both of your controllers depend on IDoStuff. Does this make sense to you?

Can a controller use the same instance of command from a controller it extends?

I have multiple SimpleFormController controllers. One controller contains a command object used to filter results in the other controllers. I seem to be missing something simple, but I can't seem to find a way to use the same instance of the command object in the other controllers.
My setup is such that this main controller, let's call it RootController extends SimpleFormController, and the rest of the controllers extend RootController. The idea was that the command object is stored in one place - RootController and the controllers that extend it reuse the same object. However, it doesn't seem to be working that way, other controllers seem to have their own copy of the command object.
Form backing objects are just normal Pojos, so you can inherit it form each other.
public class BaseCommand {
...
}
public class MoreCommand extends BaseComman {
...
}
May you just forget the "update" the commandClass in your Controller Subclasses.
Anyway: notice that SimpleFormController is deprecated in Spring 3.0. Instead the Annotation Style is preferred.
Update: One INSTANCE of an command object, can be handled by only one INSTANCE of an Controller. So you can subclass the Controller (don't miss to call super), but you can not have two instances of the controller and hope that both are invoked.

Resources