How to add your own class in Laravel 5?

Ok, in laravel 4, if I want to add my own custom class, for example: library \ myFunction.php, follow these steps:

  • add " myFunctions.php " to the application /library/myFunctiosn.php
  • app / start / global.php , in ClassLoader :: addDirectories (array ( ), I add app_path (). '/ library',
  • And to name it in my view with blades, I add the following codes
<?php $FmyFunctions1 = new myFunctions; $is_ok1=($FmyFunctions1->is_ok()); ?> 
  1. The contents of app / library / myFunctions.php:
 <?php namespace App\library { class myFunctions { public function is_ok() { return 'myFunction is OK'; } } } ?> 

And it works.

But how to do it in Laravel 5 ???

PS: I read. What are the best practices and best places for laravel 4 helpers or core functions?

And tried to add "app / library /" to the autoload array and run the composer dum-autoload , but it continues to give me error:

FatalErrorException on line xxxx xx: class 'myFunctions' not found

I am also trying to use:

 composer update composer dump-autoload php artisan dump php artisan clear-compiled php artisan dump-autoload php artisan optimize php artisan route:clear php artisan route:scan php artisan route:list 

But still does not work ...

+8
source share
2 answers

Sometimes for trial and error, I find the answer:

No need to change the composer. Just change the blade to

 <?php $FmyFunctions1 = new \App\library\myFunctions; $is_ok = ($FmyFunctions1->is_ok()); ?> 
+5
source

This should help you.

For your information: in principle, you can create another directory in the application, and then, if necessary, put the space of your files there:

application /CustomStuff/CustomDirectory/SomeClass.php.

Then, in your SomeClass.php, make sure you use the namespace:

 <?php namespace App\CustomStuff\CustomDirectory; class Someclass {} 

Now you can access this class using the namespace in your classes:

 use App\CustomStuff\CustomDirectory\SomeClass; 
+13
source

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


All Articles