Where should I put custom methods/function, when using Laravel? - php

Lets say I created a custom function called activateAuction should I put the function in my controller or in my model. If I put it in the controller, I will be writing the entire function in the controller. If I put the function in the model, I will call my function from my controller. Which is the right way as I want to write clean code?

Controllers receive a Request and return a Response. That's their only job. Keep them skinny. Skinny controllers and fat models. I don't know what activateAuction does. Maybe it should be in a Repository.

For sure you don't have to place it in the controller, keep it in the model or consider using the repository pattern, it will keep your code clean.

Controller should only be responsible for receiving a request and responding. It would validate and transform the request to pass the params to the function somewhere in your code.
You can either use Repository Pattern or just put in the Model.
You can start on writing your code in Controller, but when you find it verbose or code is getting
repetitive. You can refactor your code and make them a function, then call the function in your Controller.
Example:
class UserController {
...
public function eatCake(Request $request, Cake $cake) {
// Validate Request
$data = $this->validate($request, [
'portion' => 'required|numeric',
]);
// pass params
Auth()->user()->eat($cake, $data['portion']);
// Respond to user
return response('OK');
}
}
class User extends Authenticatable {
...
public function eat(Cake $cake, $portion) : Consume
{
// Your logic
return $this->consumes()->create([
'cake_id' => $cake_id,
'portion' => $portion
]);
}
}

Related

Why choose FormRequest authorization instead of Policies/Gates in Controller?

My question is simple, yet I couldn't find any answer to this question.
I use Policies to authorize CRUD operations.
I use Gates to authorize other non-model related actions.
I perform these authorizations in the Controller files.
...
In the meanwhile, I validate form inputs inside the FormRequest files.
My question is. Why would anyone perform authorization in the FormRequest file? Isn't it better to have it all in the controllers?
BlogPostController code:
class BlogPostController extends Controller
{
public function __construct(){
$this->middleware('auth', ['except' => ['index', 'show']]);
}
public function index()
{
// Okay, I authorize by gate here
if (\Gate::denies('example-gate-authorization')){
return redirect()->route('index');
}
$posts = BlogPost::with('user')->latest()->paginate(5);
return view('posts.index', compact("posts"));
}
public function create()
{
// Okay, I authorize by policy here
$this->authorize("create", BlogPost::class);
$post = new BlogPost();
return view('posts.createOrEdit', compact('post'));
}
// etc...
}
BlogPostRequest code:
class BlogPostRequest extends FormRequest
{
public function authorize()
{
// BUT why would anyone authorize anything in here ???
return true;
}
public function rules()
{
// Yeah the rules are okay here ..
return [
'title' => 'required|max:255|unique:posts' . ($this->post ? (',title,' . $this->post->title . ',title') : ''),
'body' => 'required'
];
}
}
Is there any practical reason when a developer would choose FormRequest authorization over Controller/Policy,Gate authorization?
(Yes, I know one can use Gates in the FormRequest file too)
Encapsulation and reusability are two arguments. Your form request object manages the authorisation and validation in a single place (encapsulation) which can be reused in other places that require the same logic (DRY).
Should your authorisation logic change, you only need change it in a single location.
As an example; consider you have a web controller with methods that require validation and authorisation through gates and policies. Your validation is in a FormRequest and your authorisation is specified in each controller action. Then you decide to extend your app to provide this functionality via an API. You now need to copy the authorisation logic to your API controller methods and remember to keep them in sync if/when changes occur. You also need to test that the authorisation workflow is functioning correctly in multiple places.
If that logic is in a FormRequest, you have a single place for this logic so only one code file to maintain and test (in a perfect world).
Does this mean you must use FormRequest objects? No. Are they helpful? Yes.
There is no difference between this discussion and the discussion around validating in a form request class vs. in the controller. They are both viable approaches, and whatever works best for you in the best solution for you.

Routes inside controllers with laravel?

I have been declaring all the routes for my application inside web.php , but it is now getting quite large. I find that I am losing a lot of time shifting between web.php and each controller and this is hurting productivity.
I feel like it would be better to define routes inside of the controller, perhaps ideally delegating some URL to a controller and then allowing the controller to handle the "sub routes" since this would allow me to use inheritance when I have two similar controllers with similar routes.
It is not possible given how laravel works. Every request is passed onto router to find its designated spot viz. the controller with the method. If it fails to find the route within the router, it just throws the exception. So the request never reaches any controller if the route is not found. It was possible in earlier versions on Symphony where you would configure the route in the comment of a particular controller method.
Sadly with laravel it works how it works.
But for me, I just like to have the routes in a separate file.
Alternate solution, easier way to sort all the routes.
You can move your route registration into controllers if you use static methods for this. The code below is checked in Laravel 7
In web.php
use App\Http\Controllers\MyController;
.....
MyController::registerRoutes('myprefix');
In MyController.php
(I use here additional static methods from the ancestor controller also posted below)
use Illuminate\Support\Facades\Route;
.....
class MyController extends Controller {
......
static public function registerRoutes($prefix)
{
Route::group(['prefix' => $prefix], function () {
Route::any("/foo/{$id}", self::selfRouteName("fooAction"));
Route::resource($prefix, self::selfQualifiedPath());
}
public function fooAction($id)
{
........
}
In Controller.php
class Controller extends BaseController {
....
protected static function selfAction($actionName, $parameters = [], $absolute = false)
{
return action([static::class, $actionName], $parameters, $absolute);
}
protected static function selfQualifiedPath()
{
return "\\".static::class;
}
protected static function selfRouteName($actionName)
{
//classic string syntax return "\\".static::class."#".$actionName;
// using tuple syntax for clarity
return [static::class, $actionName];
}
}
selfAction mentioned here is not related to your question, but mentioned just because it allows making correct urls for actions either by controller itself or any class using it. This approach helps making action-related activity closer to the controller and avoiding manual url-making. I even prefer making specific functions per action, so for example for fooAction
static public function fooActionUrl($id)
{
return self::selfAction('foo', ['id' => $id]);
}
Passing prefix into registerRoutes makes controller even portable in a sense, so allows inserting it into another site with a different prefix in case of conflict

Laravel execute multiple functions in the same controller

Is it possible to execute multiple functions in the same controller with one route. I thought it would be something like this but it doesn't work.
Route::get('getdata','controller#getData', 'controller#getData1', 'controller#getData2');
In the controller are these functions:
getData
getData1
getData2
Or is there a easier way?
In the controller
Add something like this.
class YourController extends Controller {
//...
protected function getAllData() {
//Executes the seperate functions.
$this->getData();
$this->getData1();
$this->getData2();
}
//...
}
This will execute the functions respectively.
Then from your route, you just call YourController#getAllData as the function of the controller.
It does not make sense if multiple controller actions are responsible for a single route. That's not how MVC works. You should have one and only one action for each route, and call every other function you need inside that action.
And remember, for best practices each method of the controllers must contain only the code to respond the request, not business logic, and if you have any other functions which needs to be called, put them in another other classes (layers).
class MyController extends Controller {
public function myAction(MyService $myService) {
$myService->getData();
// not $this->getData()
}
}

Laravel Service as Controller - working with multiples controllers

I'am a Brazilian developer, so... sorry for my limited English right away.
Well, in fact my problem is more a convention problem because until now I hadn't use services with Laravel (my apps were that simple so far).
I read about it before ask this question, but nothing helped with this specific situation. I'll try to describe in a objective way.
before that, just a comment: I know about the mistake using just controllers in these example. The ask is really about that mistake.
Well, the actual structure is:
abstract class CRUDController extends Controller {
protected function __construct($data, $validatorData) {
// store the data in a attribute
// create with Validator facade the validation and store too
}
abstract protected function createRecord();
protected function create() {
try {
// do the validation and return an Response instance with error messages
// if the data is ok, store in the database with models
// (here's where the magic takes place) in that store!
// to do that, calls the method createRecord (which is abstract)
$this->createRecord();
// return a success message in an Response instance
}
catch(\Exception $e) {
// return an Response instance with error messages
}
}
}
class UserController extends CRUDController {
public function __construct($data) {
parent::__construct($data, [
'rules' => [
// specific user code here
],
'messages' => [
// specific user code here
],
'customAttributes' => [
// specific user code here
]
]);
}
protected function createRecord() {
$user = new UserModel();
// store values here...
$user->save();
return $user;
}
}
// here's the route to consider in that example
Route::post('/user', 'WebsiteController#register');
class WebsiteController extends Controller {
private $request;
public function __construct(Request $request) {
$this->request = $request;
}
public function register() {
$user = new UserController();
$user->create($this->request);
// here's the problem: controller working with another controller
}
}
class UserAPIController extends Controller {
// use here the UserController too
}
and many other classes that extends CRUDController in the same way...
What I want
I want to create a controller (called here as CRUDController) to reuse methods like the pattern says (create, read, update and delete).
To be really objective here I'll use the create method as an example.
With the code above it seems clear the purpose? I think so... all my controllers have that code of validation equal and reusable. That's the thing.
Besides that, I want to my route of website call another controller (UserController) to store new users... but in the same way, I'll create an API that uses the same controller in the same way (with validations etc). That's the purpose of Responses in the CRUDController (I'll read them in the WebSiteController to resolve what to do, like show a view and in the other hand with the API I'll basically return the Response.
My real problem
Convention and pattern. The MVC pattern is broken here. Controller calling another controller is wrong and I know that.
I want to know what thing I should use! Services? Is that right? I see a lot (really) of examples of services but nothing like that, working with models and reusing code, etc. I never use Services but I know how to use, but I don't know if it's right to these cases.
I really hope that someone can help here and sorry once again for the mistakes with the English. Thanks a lot.
You're calling the CRUD controller a controller but it does not behave as an MVC controller. At best it's just a helper class. You could always do this:
abstract class CRUDManager {
//As you had the CRUDController
}
class UserManager extends CRUDManager {
//As you had the UserController
}
In your AppServiceProvider:
public function boot() {
$app->bind(UserManager::class, function ($app) {
return new UserManager(request()->all()); //I guess that's what you need.
});
}
Whenever you need to use it you can do:
public function register(UserManager $user) {
$user->create();
}
Now one thing to point out. It's not a good idea to initialise the request in the constructor. You should use dependency injection in controller methods. I don't even know if the request is available when the controller is being constructed (I know the session is not). The reason why I say this is that the middleware runs after the controller is constructed and therefore the request may be modified when the controller method is called.
Another note: If you did the original solution because you needed to use certain controller methods, then you can just use the corresponding traits (because the controller itself does not really have many method). I'm guessing a trait like ValidatesRequests would be one you'd need to use.
I'll answer my own question. I use a pattern called Repository Pattern to resolve the problem (or I try to use, because it's the first time using this pattern: maybe I don't use in the right way in every steps).
Files structure
Controllers
UserController.php
Models
UserModel.php
Providers
UserRepositoryServiceProvider.php
Repositories
RepositoryInterface.php
Repository.php
User
UserRepositoryInterface.php
UserRepository.php
Traits
InternalResponse.php
With that structure I did what I wanted in my question without working just with controllers.
I create a trait called InternalResponse. That trait contains a few methods that receive a transaction, validate if it's the case and then return a Response (called "internal" in my logic because the controller will read and maybe change the Response before return it in the end).
The Repository class, which is abstract (because another class must extend it to make sense to use. In this case the class UserRepository will extend...), uses the Trait mentioned.
Well, with it in mind, it's possible to know that the UserController uses the UserRepositoryInterface, that provides an object UserRepository: because the UserRepositoryServiceProvider register this with that interface.
I think there's no need to write code here to explain, because the problem is about an pattern, and these words explain well the problem (in the question) and the resolution with this answer here.
I'll write here a conclusion, I mean, the files structure with comments to explain a little bit more, to end the answer.
Conclusion: Files structure with comments
Controllers
UserController.php
// the controller uses dependency injection and call methods of
// UserRepository, read and changes the Response receveid to finally
// create the final Response, like returning a view or the response
// itself (in the case it's an API controller)
Models
UserModel.php
// an normal model
Providers
UserRepositoryServiceProvider.php
// register the UserRepositoryInterface to
// return a UserRepository object
Repositories
RepositoryInterface.php
// the main interface for the Repository
Repository.php
// the main repository. It's an abstract class.
// All the others repositories must extend that class, because
// there's no reason to use a class Repository without an Model
// to access the database... That class share methods like create,
// read, update and delete, and the methods validate and transaction
// too because uses the trait InternalResponse.
User
UserRepositoryInterface.php
// the interface for UserRepository class
UserRepository.php
// that class extend Repository and uses the UserModel
Traits
InternalResponse.php
// trait with methods like validate and transaction. the method
// validate, read and validate the data receveid for the methods
// create and update. and all the CRUD methods uses the method
// transaction to perform the data to the database and return a
// response of that action.
That's what I do and like I said before, I don't know if it's a hundred percent correct in reference to Repository Pattern.
I hope this can help someone else too.
Thanks for all.

Laravel call function from another controller during validation for Paypal payment

I have a registration form (using Laravel 5 here), which upon submit calls BusinessController#postRegister
public function postRegister(Requests\RegisterBusinessRequest $request)
{
#1. get input, fill new class and save to database
#2. call PayPayController#postBusinessProvider
}
What I'm trying to do is call my PayPalController function to process payment, I've tried return Redirect::action('PayPalController#postBusinessProvider'); but that doesn't seem to work.
Do I have to simply have to create a route to call a function from another Controller or is there another way without creating a route? Or should I just put my PayPal code within the postRegister function, I figured cleaner to seperate?
In MVC model (and in Laravel) a controller will not call another controller in one request. To make your controller business simpler, you can use one of these two options:
Option 1
Separate a controller logic into small parts, each part is solve by a private or protected method of the same controller class. So that your controller will be something like this:
public function postRegister(Requests\RegisterBusinessRequest $request)
{
$this->_validateInput();
$this->_processInput();
$this->_doWithPaypal();
}
Option 2
Create a repository class and to all complicated logic here. In your controller method, get the repository instance and pass your input data as the argument for this instance methods. You can see and example at zizaco/confide package. Your controller will something like this:
public function postRegister(Requests\RegisterBusinessRequest $request)
{
$repo = App::make('PaypalRepositoryClass');
// process your input
// ...
// ...
$repo->paypalBusiness($data);
}

Categories