Babak no'doust
Babak no'doust

Reputation: 641

How to define controller path in Laravel Package

I want to create a package with Laravel and Route my APIs to my package controllers. 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:

$this->app->make('Devdojo\Calculator\CalculatorController');

Thanks.

Upvotes: 1

Views: 1377

Answers (2)

katsarov
katsarov

Reputation: 1822

You only need in your PackageServiceProvider.php

    public function register()
    {
        include __DIR__ . '/routes.php';
    }

And routes.php of course.

Upvotes: 1

Babak no'doust
Babak no'doust

Reputation: 641

After a while I found it. So the i do this:

1- Copy Laravel RouteServiceProvider to my package

2- rename it to my own Package Name

3- remove some unusable method and the finaly my RouteServiceProvider id following

<?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

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:

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:

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

Upvotes: 2

Related Questions