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.

weekapaug
  • 332
  • 1
  • 4
  • 15
  • Laravel apps use composer so you can use https://stackoverflow.com/questions/24171078/composer-psr-how-to-autoload-functions – apokryfos Apr 15 '18 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 – weekapaug Apr 15 '18 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 – apokryfos Apr 15 '18 at 19:17
  • Also if performance is a concern then enable OpCache . Loading a file when OpCache is enabled is not an expensive operation. – apokryfos Apr 15 '18 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 – weekapaug Apr 15 '18 at 19:23

1 Answers1

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(); 
    }
}
weekapaug
  • 332
  • 1
  • 4
  • 15
Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7
  • Where does part 1 of this answer go? Where do I put namespace App\Helpers; classHelpersOne{public function(){../*my code*/}} – weekapaug Apr 15 '18 at 19:32
  • @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 – Mike Foxtech Apr 15 '18 at 19:35
  • 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 Apr 15 '18 at 19:38
  • @weekapaug Exactly – Mike Foxtech Apr 15 '18 at 19:40
  • 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 – weekapaug Apr 15 '18 at 19:50
  • @weekapaug Have you created methods with your features? If you need to submit any changes to the Helpers classes, you can pass them as method arguments – Mike Foxtech Apr 15 '18 at 19:52
  • top of controller has Use App\Helpers - the file inside App\Helpers has class of HelpersOne then inside class is myFunctionOne. Inside controller public function I put $myFunction = new HelperOne();$myFunction->myFunctionOne();but for some reason its still looking in the controller folder not the app one – weekapaug Apr 15 '18 at 19:57
  • @weekapaug What exactly looks at the controller, I do not understand the question – Mike Foxtech Apr 15 '18 at 20:02
  • I dont know how to display it all in a comment. I honestly thought this could be solved in a much simpler way so I'll just leave it in the controller in all its ugly glory until I figure it out myself. This did not work for me. – weekapaug Apr 15 '18 at 20:10
  • @weekapaug so of course, in order not to waste time, let it be so) – Mike Foxtech Apr 15 '18 at 20:18
  • I know you are on the right track and its probably something simple too, so I will probably stumble onto whats wrong when I'm not trying so hard :) – weekapaug Apr 15 '18 at 20:21
  • Well I researched it more and found that I needed to reference the directory one level deeper as in use App\Helpers\HelpersOne; So your code in the example would be perfect if it just went to the full class path and not just App\Helpers as written, thanks for leading me in the right direction – weekapaug Apr 15 '18 at 20:49
  • You were not wrong, just one small piece of info missing, but you still helped me, so thanks – weekapaug Apr 15 '18 at 20:58