简体   繁体   English

Laravel控制器和包装中的路线?

[英]Laravel controllers and routes in packages?

I'm having trouble registering controllers to routes in my package. 我在将控制器注册到包中的路由时遇到了问题。

As far as I understand from the official documentation I just need to simply prefix it and use my namespace though I always get the error of xyzclass not found. 据我从官方文档中理解,我只需要简单地为它添加前缀并使用我的命名空间,但我总是得到xyzclass的错误。

Anyone could explain to me how can I make this work? 任何人都可以向我解释如何才能完成这项工作?

-- Vendor -- Package |-- composer.json |-- composer.lock |-- public |-- src | |-- Vendor | | -- Package | | -- PackageServiceProvider.php | |-- config | |-- controllers | | -- MyController.php | |-- lang | |-- migrations | |-- routes.php | -- views |-- tests

I loaded my routes.php in my serviceprovider file. 我在我的serviceprovider文件中加载了routes.php。 I created and index function in MyController.php and assigned it in my routes.php. 我在MyController.php中创建并编制了索引函数,并将其分配给我的routes.php。

Route::contorller('myctrl', 'Vendor\Package\MyController');

And in theory I should be able to access mydomain.com/myctrl and it should give me back what I defined in MyController's index function. 理论上我应该能够访问mydomain.com/myctrl,它应该让我回到我在MyController的索引函数中定义的内容。 But in reality I get an error saying Class Vendor\\Package\\MyController does not exists. 但实际上我得到一个错误,说Class Vendor \\ Package \\ MyController不存在。 Any advice or help how should this work or how it's working? 任何建议或帮助如何工作或如何工作?

Route::contorller('myctrl', 'MyController');

这与src/controllers/链接到vendor

Guess I'm dumb, forgot some little details. 猜猜我是笨蛋,忘记了一些小细节。 I should use a proper namespace next time, and adding the controllers folder to my composer.json helped too. 我下次应该使用一个合适的命名空间,并将controllers文件夹添加到我的composer.json也有帮助。

I found a nice and detailed description on how to do this here: https://stackoverflow.com/a/18137352/1397967 我在这里找到了一个很好的详细描述: https//stackoverflow.com/a/18137352/1397967

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM