在 Laravel 4 中调用未定义的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17358683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:02:37  来源:igfitidea点击:

Call to undefined function in Laravel 4

functionlaravellaravel-4

提问by user1072337

I have set up a resource in Laravel 4 for an entity "Artists"; Within the ArtistController, I have added a function of my own, youtube_embed. When I call this function in my view (the show view), it is saying that it is undeclared. Do you have any idea why I am receiving this error? Thank you.

我在 Laravel 4 中为实体“艺术家”设置了一个资源;在 ArtistController 中,我添加了一个我自己的函数 youtube_embed。当我在我的视图(显示视图)中调用这个函数时,它说它是未声明的。你知道为什么我会收到这个错误吗?谢谢你。

Here is the code:

这是代码:

in ArtistController:

在 ArtistController 中:

 public function youtube_embed($vari) {

        $step1=explode('v=', $vari);
        $step2 =explode('&',$step1[1]);
        $iframe ='<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';

        return $iframe;

    }

in the show.blade.php:

在 show.blade.php 中:

{{youtube_embed($artist->video_path);}}

Thank you again for your help.

再次感谢你的帮助。

回答by Maktouch

There's lot of way to achieve this.

有很多方法可以实现这一目标。

  • You could include the file at the bottom of app/start/global.php

  • You could make a helpers class

    1. Create a folder in app called "helpers" along with a file called Embed.php
    2. add "app/helpers" to composer.json in the "autoload.classmap" section. Watch out for commas.
    3. In app/start/global.php, add app_path().'/helpers' in the ClassLoader::addDirectories array. This step is not necessary but it prevents you from doing composer dump-autoload everytime you add a new helper.
    4. in app/helpers/Embed.php, you could do something like this

      class Embed {
        public static function youtube($vari) {
      
          $step1 = explode('v=', $vari);
          $step2 = explode('&amp;', $step1[1]);
          $iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
      
          return $iframe;
        }
      }
      

    And use it in blade like this

    {{Embed::youtube($artist->video_path)}}
    

    That way, if you want to add embed vimeo, you could also add it and call it like

    {{Embed::vimeo($artist->video_path)}}
    
  • You could make a custom form macro

    Form::macro('youtube', function($vari) {
       $step1 = explode('v=', $vari);
       $step2 = explode('&amp;', $step1[1]);
       $iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
    
        return $iframe;
    });
    

    and call it like this

    {{ Form::youtube($artist->video_path) }}
    
  • 您可以在 app/start/global.php 的底部包含该文件

  • 你可以做一个助手类

    1. 在应用程序中创建一个名为“helpers”的文件夹以及一个名为 Embed.php 的文件
    2. 在“autoload.classmap”部分将“app/helpers”添加到composer.json。注意逗号。
    3. 在 app/start/global.php 中,在 ClassLoader::addDirectories 数组中添加 app_path().'/helpers'。此步骤不是必需的,但它会阻止您在每次添加新帮助程序时执行 composer dump-autoload。
    4. 在 app/helpers/Embed.php 中,你可以做这样的事情

      class Embed {
        public static function youtube($vari) {
      
          $step1 = explode('v=', $vari);
          $step2 = explode('&amp;', $step1[1]);
          $iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
      
          return $iframe;
        }
      }
      

    并像这样在刀片中使用它

    {{Embed::youtube($artist->video_path)}}
    

    这样,如果你想添加嵌入 vimeo,你也可以添加它并像这样调用它

    {{Embed::vimeo($artist->video_path)}}
    
  • 您可以制作自定义表单宏

    Form::macro('youtube', function($vari) {
       $step1 = explode('v=', $vari);
       $step2 = explode('&amp;', $step1[1]);
       $iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
    
        return $iframe;
    });
    

    并像这样称呼它

    {{ Form::youtube($artist->video_path) }}
    

So many possibilities! :)

这么多的可能性!:)

回答by Edwin Aw

Looks like all you need is just a helper. I would do this in my app.

看起来你所需要的只是一个帮手。我会在我的应用程序中执行此操作。

Firstly, create a folder "helpers" in your "app" folder.

首先,在您的“app”文件夹中创建一个文件夹“helpers”。

Then create a file named "common.php" inside the "helpers" folder. Put your function in this file:

然后在“helpers”文件夹中创建一个名为“common.php”的文件。把你的函数放在这个文件中:

    <?php

        if ( ! function_exists('image'))
        {
            function youtube_embed($vari)
            {
                 $step1=explode('v=', $vari);
                         $step2 =explode('&amp;',$step1[1]);
                         $iframe ='<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';

                         return $iframe;
            }
         }

Next, include this file in your route.php.

接下来,将此文件包含在您的 route.php 中。

  <?php

      include('libraries/common.php');

And you can use your function inside your application.

您可以在应用程序中使用您的函数。