在laravel 5.5中创建一个自定义外部类

时间:2018-02-07 14:16:03

标签: php laravel

我想在Laravel中创建一个外部类,我想在我的cotroller函数中使用这个类。

最好的方法是什么?

感谢

2 个答案:

答案 0 :(得分:4)

创建一个类并定义其命名空间。例如:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.no-wrdbrk{
word-break:keep-all;
}
</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="word-break:keep-all;" class="no-wrdbrk">
Several new studies have found that yoga may lower depression and emotional eating, if done on a consistent basis. At the 125th Annual Convention of the American Psychological Association, four separate studies were presented that pointed to similar positive findings about the benefits of yoga.
</td>
</tr>
</table>
</body>
</html>

运行<?php namespace App\Services; class MyClass { public function doSomething() { dd('It\'s working'); } } 命令。

您可以在控制器中使用该类:

composer du

IoC

(new App\Services\MyClass)->doSomething();

如果您正在使用IoC,您还可以将该类注入控制器构造函数:

app('App\Services\MyClass')->doSomething();

答案 1 :(得分:0)

使用常用功能任何文件,如控制器,型号和所有刀片文件都有效。

请尝试创建内部的helpers.php文件。

文件路径如:laravel / app / helpers.php

代码

if (!function_exists('classActivePath')) {

   function classActivePath($path) {
       return Request::is($path) ? ' class="active"' : '';
   }

}
相关问题