如何在Laravel 5中添加自己的自定义类? [英] How to add my own custom class in Laravel 5?
问题描述
好吧,在laravel 4中,如果我想添加自己的自定义类,例如:library\myFunction.php,则执行以下步骤:
- 将 myFunctions.php 添加到 app / library / myFunctiosn.php
- 在 app / start / global.php 的 ClassLoader :: addDirectories(array( app_path()。'/ library',
- 要在我的刀片视图中调用它,请添加以下代码
<?php
$ FmyFunctions1 = new myFunctions ;
$ is_ok1 =($ FmyFunctions1-> is_ok());
?>
- app / library / myFunctions.php的内容为:
< pre class = lang-html prettyprint-override> <?php命名空间App\library {
class myFunctions {
public function is_ok(){
返回 myFunction正常;
}
}
}
?>
而且有效。
但是如何在Laravel 5中这样做呢?
PS:我读了 laravel 4 helper或的最佳实践和最佳位置是什么?基本功能?
并尝试将 app / library /, 添加到 自动加载数组 并运行 composer dum-autoload ,但它总是给我错误:
xxxx行xx中的FatalErrorException:未找到类'myFunctions'
我也已经在尝试使用:
composer update
composer dump-autoload
php artisan dump
php artisan清除编译的
php artisan dump-autoload
php artisan优化
php artisan路线:clear
php artisan路线:扫描
php artisan路线:列出
但仍然无效。
经过反复试验,我找到了答案。
无需修改Composer。只需将Blade修改为:
<?php
$ FmyFunctions1 = new \ \应用程序库\我的功能;
$ is_ok =($ FmyFunctions1-> is_ok());
?>
Ok, in laravel 4, if I want to add my own custom class, eg : library\myFunction.php then I do the following steps :
- add "myFunctions.php" into app/library/myFunctiosn.php
- at app/start/global.php , within ClassLoader::addDirectories(array( , I add app_path().'/library',
- And to call it within my blade view, I add the following codes
<?php
$FmyFunctions1 = new myFunctions;
$is_ok1=($FmyFunctions1->is_ok());
?>
- The contents of app/library/myFunctions.php is :
<?php namespace App\library {
class myFunctions {
public function is_ok() {
return 'myFunction is OK';
}
}
}
?>
And it works.
But how to do so in Laravel 5 ???
PS : I read What are the best practices and best places for laravel 4 helpers or basic functions?
And tried to add "app/library/", to the autoload array and run composer dum-autoload , but it keeps give me error :
FatalErrorException in xxxx line xx: Class 'myFunctions' not found
I'm also already 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 doesn't work...
After some trial and error, I found the answer.
There is no need to modify Composer. Just modify the Blade into:
<?php
$FmyFunctions1 = new \App\library\myFunctions;
$is_ok = ($FmyFunctions1->is_ok());
?>
这篇关于如何在Laravel 5中添加自己的自定义类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!