dscss8996 2017-10-25 18:03
浏览 70
已采纳

@include相当于Laravel中的控制器

I have various controller functions where I have a large chunk of code (for an API call) which repeats in other functions a lot of time. Is there a @include equivalent to just copy/paste the codes in my controller. This will be much easier to read and follow through.

In my controller, something like this

    public function store () 
    {
       if ($company->name = 'XYX')
       {
           @include('xyzcontrollercode') 
       }

       if ($company->name = 'DEF')
       {
           @include('defcontrollercode') 
       }
   }

The includes - 'xyzcontrollercode' will have a large chunk of logic which will implement once the 'if' condition matches.

Any way to achieve this kind of functionality for controllers?

  • 写回答

1条回答 默认 最新

  • duancunsu9209 2017-10-25 18:46
    关注

    You can create a general controller and inherit from it.

    For example:

    class GeneralController extends Controller
    {
        public function operation(){
            // do some things ...
        }
    }
    
    class HomeController extends GeneralController
    {
        public function store(){
            // do some things ...
    
            if ($company->name == 'XYX')
            {
                $this->operation(); 
            }
    
            // do something ...
        }
    }
    

    Or you can use dependency injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)