Laravel类型错误:函数参数太少

时间:2018-06-22 11:19:51

标签: laravel-5.5

我用索引控制器创建了一个控制器:

[INFO  ] (DefaultLifecycleProcessor.java:343) org.springframework.context.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018/06/22 13:56:47,454 [INFO  ] (DocumentationPluginsBootstrapper.java:151) springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper : Context refreshed
2018/06/22 13:56:47,504 [INFO  ] (DocumentationPluginsBootstrapper.java:154) springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2018/06/22 13:56:47,555 [INFO  ] (ApiListingReferenceScanner.java:41) springfox.documentation.spring.web.scanners.ApiListingReferenceScanner : Scanning for api listing references
2018/06/22 13:56:47,826 [INFO  ] (DirectJDKLog.java:180) org.apache.coyote.http11.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-8080"]
2018/06/22 13:56:47,827 [INFO  ] (DirectJDKLog.java:180) org.apache.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-8080"]
2018/06/22 13:56:47,829 [INFO  ] (DirectJDKLog.java:180) org.apache.tomcat.util.net.NioSelectorPool : Using a shared selector for servlet write/read
2018/06/22 13:56:47,836 [INFO  ] (TomcatEmbeddedServletContainer.java:201) org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018/06/22 13:56:47,902 [INFO  ] (AbstractConnectionFactory.java:481) org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory : started 180.112.19.1153115.inorg.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory#0, host=180.112.19.115, port=3115
2018/06/22 13:56:47,922 [INFO  ] (AbstractEndpoint.java:120) org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter : started org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter#0
2018/06/22 13:56:47,949 [INFO  ] (EventDrivenConsumer.java:108) org.springframework.integration.endpoint.EventDrivenConsumer : Adding {ip:tcp-outbound-channel-adapter} as a subscriber to the '180.112.19.1153115.out.input' channel
2018/06/22 13:56:47,950 [INFO  ] (AbstractSubscribableChannel.java:81) org.springframework.integration.channel.DirectChannel : Channel 'ck-da-bridge:local:8080.180.112.19.1153115.out.input' has 1 subscriber(s).
2018/06/22 13:56:47,950 [INFO  ] (AbstractConnectionFactory.java:481) org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory : started 180.112.19.1153115.inorg.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory#0, host=180.112.19.115, port=3115
2018/06/22 13:56:47,950 [INFO  ] (AbstractEndpoint.java:120) org.springframework.integration.endpoint.EventDrivenConsumer : started org.springframework.integration.config.ConsumerEndpointFactoryBean#1
2018/06/22 13:56:47,953 [INFO  ] (StartupInfoLogger.java:57) com.santander.ck.bridge.spring.Application : Started Application in 13.078 seconds (JVM running for 14.159)
2018/06/22 13:57:01,164 [ERROR ] (LoggingHandler.java:192) org.springframework.integration.handler.LoggingHandler : org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'ck-da-bridge:local:8080.180.112.19.1153115.in.channel#0'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=f0f8f0f0c2200000800000020400000000000000f0f5f9f9f1f0f3f0f6f2f2f1f3f5f6f5f5f0f0f0f0f1f2f0f6f0f2f2f0f2f0f0f0f9d4c3c3f0f1f1f1e3f0f0f8f2, headers={ip_tcp_remotePort=3115, ip_connectionId=amm012059amm.scisb.isban.corp:3115:53402:3722e84f-49ce-4e77-a4a1-6e82cefe58b4, ip_localInetAddress=/180.101.130.119, ip_address=180.112.19.115, id=2c6e2e09-9faa-a56e-3921-edf7a03a9a3c, ip_hostname=amm012059amm.scisb.isban.corp, timestamp=1529668621142}], failedMessage=GenericMessage [payload=f0f000022f0f6e3f0f0f8f2, headers={ip_tcp_remotePort=3115, ip_connectionId=amm012059amm.scisb.isban.corp:3115:53402:3722e84f-49ce-4e77-a4a1-6e82cefe58b4, ip_localInetAddress=/180.101.130.119, ip_address=180.112.19.115, id=2c6e2e09-9faa-a56e-3921-edf7a03a9a3c, ip_hostname=amm012059amm.scisb.isban.corp, timestamp=1529668621142}]
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:93)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:425)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:375)
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
        at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
        at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
        at org.springframework.integration.endpoint.MessageProducerSupport.sendMessage(MessageProducerSupport.java:188)
    at org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter.onMessage(TcpReceivingChannelAdapter.java:88)
    at org.springframework.integration.ip.tcp.connection.TcpNetConnection.run(TcpNetConnection.java:182)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=f0f8f0f0c2200000800000020400000000000000f0f5f9f9f1f0f3f0f6f2f2f1f3f5f6f5f5f0f0f0f0f1f2f0f6f0f2f2f0f2f0f0f0f9d4c3c3f0f1f1f1e3f0f0f8f2, headers={ip_tcp_remotePort=3115, ip_connectionId=amm012059amm.scisb.isban.corp:3115:53402:3722e84f-49ce-4e77-a4a1-6e82cefe58b4, ip_localInetAddress=/180.101.130.119, ip_address=180.112.19.115, id=2c6e2e09-9faa-a56e-3921-edf7a03a9a3c, ip_hostname=amm012059amm.scisb.isban.corp, timestamp=1529668621142}]
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:154)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89)
    ... 11 more

在EntityRepository中,我创建了getEntitiesWithKeyneeds:

public function index(EntityRepository $vcs, EntityRepository $vcskns)
{
    $entitiesLists = $vcs->getEntities();
    $entitiesWithKnLists = $vcskns->getEntitiesWithKeyneeds();

    return view('admin.home', compact('entitiesLists', 'entitiesWithKnLists'));
}

我收到一条错误消息:

类型错误:函数App \ Http \ Controllers \ BackOffice \ StatsController :: index()的参数太少,传递了1个,恰好期望有2个

我希望我能解决这个问题。

另一个问题是要知道如何迫使后端的每个视图都具有EntityRepository的返回值。从理论上讲,我必须在所有方法中声明我的变量...

1 个答案:

答案 0 :(得分:0)

我必须以这种方式创建接口:

<?php
    namespace App\Contracts;

    interface EntityInterface {
        public function getEntities();
        public function getEntitiesWithKeyneeds();
}

我的EntityRepository看起来像这样:

<?php

namespace App\Http\Controllers\BackOffice\Repository;

use App\Contracts\EntityInterface;
use App\Segment;
use App\Valuechain;


class EntityRepository implements EntityInterface
{
    public function getEntities()
    {
        $vcs = Valuechain::select('valuechains.id', 'lang_valuechain.vcname',     
                    'lang_valuechain.vcshortname')
                    ->withCount('segments')
                    ->join('sectors', 'valuechains.sector_id', '=', 'sectors.id')
                    ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
                    ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
                    ->where('langs.isMainlanguage', '=', '1')
                    ->whereNull('valuechains.deleted_at')
                    ->whereNull('sectors.deleted_at')
                    ->get();

        return $vcs;
    }

    public function getEntitiesWithKeyneeds()
    {
        // liste des cdv
        $valuechains = Valuechain::orderBy('valuechains.id')
                ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
                ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
               ->where('langs.isMainlanguage', '=', '1')
                ->with('segments')
                ->with('keyneeds')
                ->withCount('segments')
                ->withCount('keyneeds')
                ->get();

        foreach ($valuechains as $valuechain) {
            $ids[] = $valuechain->id;
        }

        foreach ($ids as $id) {
            $vcskns[] = Segment::select(
                'lang_valuechain.vcname', 'lang_valuechain.vcshortname',
                'lang_segment.segname', 'lang_segment.segshortname', 'segments.id',
                'lang_segment.created_at', 'lang_segment.updated_at', 
                'lang_segment.deleted_at'
            )
                ->distinct()
                ->withCount('keyneeds')
                ->join('lang_segment', 'segments.id', '=', 'lang_segment.segment_id')
                ->join('valuechains', 'segments.valuechain_id', '=', 'valuechains.id')
                ->join('lang_valuechain', 'valuechains.id', '=', 'lang_valuechain.valuechain_id')
                ->join('sectors', 'valuechains.sector_id', '=', 'sectors.id')
                ->join('lang_sector', 'sectors.id', '=', 'lang_sector.sector_id')
                ->join('langs', 'lang_valuechain.lang_id', '=', 'langs.id')
                ->where([
                    ['langs.isMainlanguage', '=', '1'],
                    ['valuechains.id', '=', $id]
                ])
                ->whereNull('valuechains.deleted_at')
                ->whereNull('sectors.deleted_at')
                ->whereNull('segments.deleted_at')
                ->get();
        }
        return $vcskns;

    }
}

在app / Providers / AppServiceProvider.php中,我必须绑定接口。我想我必须使用Illuminate \ Support \ ServiceProvider覆盖构造函数:

public function __construct($app)
{
    $this->app = $app;

    $this->app->bind(
        'App\Contracts\EntityInterface',
        'App\Http\Controllers\BackOffice\Repository\EntityRepository'
    );
}

使用这些值的控制器看起来像这样(这是我的索引方法):

public function index(EntityInterface $vcs)
{
    $entitiesLists = $vcs->getEntities();
    $entitiesWithKnLists = $vcs->getEntitiesWithKeyneeds();

    return view('admin.home', compact('entitiesLists', 'entitiesWithKnLists'));
}