0

In order to consolidate some bulky code in a controller I have been able to do this inside function

public function myCustomFunction(Request $request){
 include(app_path() . '/myFunctions/myCustomFunction1.php');
}

It does successfully work but by doing the include, I noticed it doesnt carry the namespace and/or other variables set in the controller.

If its just a simple model it needs I was able to add

Use \App\myModel;

to the top of the controller and it runs the code just fine as if its part of the controller.

What I am having issue with is when I want to run validation. I cant figure out what to "use" to work on the included page.

If I run my logic in the controller without doing include it works, but if put inside the include it does not, and I get NO errors either.

I tried this inside the include page to activate the validation from within the include file

namespace App\Http\Controllers;
use Validator;
use Request;

which mimics the original controller, but still does not work. Is what I'm trying to do possible but I'm doing something wrong? Or am I doing something that will never work? If all else fails I can keep the code within the actual controller to make it work, but I'm trying to accomplish some sort of clarity on complex controllers. Such as...

public function myCustomFunction(Request $request){
    if(request('value1')=='1'){
      include(app_path() . '/myFunctions/myCustomFunction1.php');
    }else{
      include(app_path() . '/myFunctions/myCustomFunction2.php');
    }
}

This structure is already working on anything that doesn't need validation so I'm hoping there is a simple way to hook the include page into the same set of validation tools the original controller has access to.

5
  • Laravel apps use composer so you can use stackoverflow.com/questions/24171078/… Commented Apr 15, 2018 at 19:11
  • Ya, I dont want to autoload it every time. It is only used rarely so I'm interested in a solution that lets me manually include it only when needed Commented Apr 15, 2018 at 19:17
  • Put the functions in a class and let the autoloading handle it. Autloading via the PSR-4 autoloader (which composer also has) is lazy as far as I know Commented Apr 15, 2018 at 19:17
  • Also if performance is a concern then enable OpCache . Loading a file when OpCache is enabled is not an expensive operation. Commented Apr 15, 2018 at 19:19
  • Thanks, I'm new to laravel and this is over my head. I thought maybe if i knew the proper directories to "use" at top of include page I could solve, but just leaving the whole mess in the controller is not an issue, just ugly Commented Apr 15, 2018 at 19:23

1 Answer 1

2

In the controller, so include the files incorrectly. It's best for you to create a Helpers folder in the app folder, in it create classes with your functions.

namespace App\Helpers;

class HelpersOne {
  public function myFunctionOne(){.../*youre code*/}
  public function myFunctionTwo(){.../*youre code*/}
}

And already in your controller you can call these classes.

use App\Helpers\HelpersOne;
...
public function myCustomFunction(Request $request){
    if(request('value1')=='1'){
      $myFunction = new HelpersOne();
      $myFunction->myFunctionOne(); 
    }else{
      $myFunction = new HelpersTwo();
      $myFunction->myFunctionTwo(); 
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

Where does part 1 of this answer go? Where do I put namespace App\Helpers; classHelpersOne{public function(){../*my code*/}}
@weekapaug In the app folder, create a Helpers folder. In the Helpers folder, create HelperOne.php for example. And already in this file you install this namespace
OK i got the helpers folder and I made the file helpersOne.php inside. So inside HelpersOne.php do I put the namespace App\Helpers,etc.?
@weekapaug Exactly
OK so I did exactly as stated and I get this... app\http\controllers\helperone not found. Its not a typo, I verified case sensitivity. Its not in the controllers folder its in app folder
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.