Sharing method between Laravel 5.2 controllers

In several controllers, I have to use the same method to display the results as a table with a column sorting function:

public function showSearchResults(Request $req){ $query=Service::where('title', $req->search); // Columns sorting if ($req->has('order')){ $order= $req->order=='asc' ? 'asc' : 'desc'; $order_inverse=$req->order=='asc' ? 'desc' : 'asc'; } else { $order='desc'; $order_inverse='asc'; } ... $url=$req->url().'?'.http_build_query($req->except('sortby','order','page')); $results=$query->with('type')->paginate(15)->appends($req->all()); return View::make('services.search_results') ->with('results', $results) ->with('url',$url) ->with('sortby', $sortby) ->with('order', $order) ->with('order_inverse', $order_inverse); } 

What is the best way to avoid DRY in this case?

+5
source share
4 answers

Sharing Methods Among Feature Controllers

Step 1: Create a Feature

 <?php // Code in app/Traits/MyTrait.php namespace App\Traits; trait MyTrait { protected function showSearchResults(Request $request) { // Stuff } } 

Step 2: use Property in your controller:

 <?php // Code in app/Http/Controllers/MyController.php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Traits\MyTrait; // <-- you'll need this line... class MyController extends Controller { use MyTrait; // <-- ...and also this line. public function getIndex(Request $request) { // Now you can call your function with $this context $this->showSearchResults($request); } } 

Now you can use use your trait in any other controller in the same way.

It is important to note that you do not need to include or require your Trait file anywhere, PSR-4 autoload takes care of including the file.

You can also use the Custom Helper classes that others have talked about, but I would recommend them if you are only going to share code between controllers. Here you can see how to create custom helper classes .

+9
source

You can use traits (as suggested as a sentence), or you can create a helpers file and add helpers there.

After that, you can use this helper from any class (controllers, models, custom classes, commands, etc.), like any other Laravel helper .

+2
source

Use traits? More available here: http://php.net/manual/en/language.oop5.traits.php

0
source

You can create a helper file in the application directory. eg. MethodHelper.php

In this file you can specify the method that you want to use anywhere. For instance,

 <?php namespace App; class MethodHelper { public static function reusableMethod() { //logic } } 

You can use this method anywhere by using the namespace and calling the method. In the above example, for example. Namespace:

The method call function will look like this:

 MethodHelper::reusableMethod(); 

You can also send parameters based on your functional requirements. In your example. could you

 public function showSearchResults(Request $req){ // } 

instead of reusableMethod (). Your call will be as follows:

 MethodHelper::showSearchResults($req); 
0
source

Source: https://habr.com/ru/post/1247874/


All Articles