[英]How to define controller path in Laravel Package
I want to create a package with Laravel and Route my APIs to my package controllers. 我想使用Laravel创建一个程序包,并将我的API路由到我的程序包控制器。 Without theirs Folder address.
没有他们的文件夹地址。 So I need something like this:
所以我需要这样的东西:
Route::prefix('message')->group(function () {
Route::get('/', 'CustomMessageController@index');
});
And I fount somthing like this: 我发现这样的东西:
Route::prefix('message')->group(function () {
Route::get('/', 'Http/Controllers/CustomMessageController@index');
});
In addition I want to define all controller automatically in the ServiceProvider 'boot' method but i can find just somthing like this: 另外,我想在ServiceProvider'boot'方法中自动定义所有控制器,但是我只能找到类似以下内容:
$this->app->make('Devdojo\Calculator\CalculatorController');
Thanks. 谢谢。
After a while I found it. 一段时间后,我发现了它。 So the i do this:
所以我这样做:
1- Copy Laravel RouteServiceProvider to my package 1-将Laravel RouteServiceProvider复制到我的包中
2- rename it to my own Package Name 2-重命名为我自己的包名称
3- remove some unusable method and the finaly my RouteServiceProvider id following 3-删除一些不可用的方法并最终删除我的RouteServiceProvider ID
<?php
namespace Alive2212\LaravelMessageService\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class AliveLaravelMessageServiceRouteServiceProvider extends ServiceProvider
{
protected $namespace = 'Alive2212\LaravelMessageService\Http\Controllers';
public function boot()
{
parent::boot();
}
public function map()
{
$this->mapApiRoutes();
}
protected function mapApiRoutes()
{
Route::prefix('alive_api')
->namespace($this->namespace)
->group(__DIR__.'/../../routes/api.php');
}
}
4- register it into my Package Service Provider and finaly my package provider 4-将其注册到我的包裹服务提供商中,最后确定我的包裹提供商
public function boot()
{
$this->app->register(AliveLaravelMessageServiceRouteServiceProvider::class);
}
5- In root of package create 'route' folder and create api.php and put some Routes into it: 5-在软件包的根目录中,创建“ route”文件夹,并创建api.php并将一些Routes放入其中:
Route::prefix('message')->group(function () {
Route::get('/', 'CustomMessageController@index');
});
6- Create Folder 'Http\\Controllers' into 'src' folder of package and put Controller.php from Laravel project and Create another controller into it somthing like this: 6-在文件夹的``src''文件夹中创建文件夹``Http \\ Controllers''并将Laravel项目中的Controller.php放入其中,并创建另一个控制器,如下所示:
namespace Alive2212\LaravelMessageService\Http\Controllers;
use Alive2212\LaravelMessageService\Http\Controllers\Controller;
use Alive2212\LaravelMessageService\Message;
use Illuminate\Http\Request;
class CustomMessageController extends Controller
{
protected $model;
public function __construct()
{
$this->model = new Message();
}
public function index(Request $request)
{
return "the is my package index controller";
}
}
7- after serve project, "localhost:8000/alive_api/message" address work correctly 7-服务项目结束后,“ localhost:8000 / alive_api / message”地址正常工作
You only need in your PackageServiceProvider.php 您只需要在PackageServiceProvider.php中
public function register()
{
include __DIR__ . '/routes.php';
}
And routes.php of course. 当然还有routes.php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.