Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit c3742d4

Browse files
author
Alex Giuvara
committed
Add Laravel 5.4 api and web routes
1 parent ec86190 commit c3742d4

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

src/Generators/ServiceGenerator.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ServiceGenerator extends Generator
3939
'resources/',
4040
'resources/lang/',
4141
'resources/views/',
42+
'routes',
4243
'Tests/',
4344
'Tests/Features/',
4445
];
@@ -51,7 +52,7 @@ public function generate($name)
5152
$path = $this->findServicePath($name);
5253

5354
if ($this->exists($path)) {
54-
throw New Exception('Service already exists!');
55+
throw new Exception('Service already exists!');
5556

5657
return false;
5758
}
@@ -65,7 +66,7 @@ public function generate($name)
6566

6667
$this->addServiceProviders($name, $slug, $path);
6768

68-
$this->addRoutesFile($name, $slug, $path);
69+
$this->addRoutesFiles($name, $slug, $path);
6970

7071
$this->addWelcomeViewFile($path);
7172

@@ -151,21 +152,27 @@ public function createRouteServiceProvider($name, $path, $slug, $namespace)
151152
$this->createFile($path.'/Providers/RouteServiceProvider.php', $content);
152153
}
153154

154-
/**
155-
* Add the routes file.
155+
/**
156+
* Add the routes files.
156157
*
157158
* @param string $name
158159
* @param string $slug
159160
* @param string $path
160161
*/
161-
public function addRoutesFile($name, $slug, $path)
162+
public function addRoutesFiles($name, $slug, $path)
162163
{
163-
$controllers = 'src/Services/'.$name.'/Http/Controllers';
164+
$controllers = 'src/Services/' . $name . '/Http/Controllers';
165+
166+
$contentApi = file_get_contents(__DIR__ . '/stubs/routes-api.stub');
167+
$contentApi = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $contentApi);
168+
169+
$contentWeb = file_get_contents(__DIR__ . '/stubs/routes-web.stub');
170+
$contentWeb = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $contentWeb);
164171

165-
$content = file_get_contents(__DIR__.'/stubs/routes.stub');
166-
$content = str_replace(['{{slug}}', '{{controllers_path}}'], [$slug, $controllers], $content);
172+
$this->createFile($path . '/routes/api.php', $contentApi);
173+
$this->createFile($path . '/routes/web.php', $contentWeb);
167174

168-
$this->createFile($path.'/Http/routes.php', $content);
175+
unset($contentApi, $contentWeb);
169176
}
170177

171178
/**

src/Generators/stubs/routes-api.stub

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Service - API Routes
6+
|--------------------------------------------------------------------------
7+
|
8+
| Here is where you can register all of the routes for this service.
9+
| It's a breeze. Simply tell Laravel the URIs it should respond to
10+
| and give it the controller to call when that URI is requested.
11+
|
12+
*/
13+
14+
// Prefix: /api/{{slug}}
15+
Route::group(['prefix' => '{{slug}}'], function() {
16+
17+
// The controllers live in {{controllers_path}}
18+
// Route::get('/', 'UserController@index');
19+
20+
Route::get('/', function() {
21+
return response()->json(['path' => '/api/{{slug}}']);
22+
});
23+
24+
Route::middleware('auth:api')->get('/user', function (Request $request) {
25+
return $request->user();
26+
});
27+
28+
});

src/Generators/stubs/routes.stub renamed to src/Generators/stubs/routes-web.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
|--------------------------------------------------------------------------
5-
| Service Routes
5+
| Service - Web Routes
66
|--------------------------------------------------------------------------
77
|
88
| Here is where you can register all of the routes for this service.

src/Generators/stubs/routeserviceprovider.stub

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ use {{foundation_namespace}}\Providers\RouteServiceProvider as ServiceProvider;
77
class RouteServiceProvider extends ServiceProvider
88
{
99
/**
10-
* Read the routes from the "routes.php" file of this Service
10+
* Read the routes from the "api.php" and "web.php" files of this Service
1111
*
1212
* @param \Illuminate\Routing\Router $router
1313
*/
1414
public function map(Router $router)
1515
{
1616
$namespace = '{{controllers_namespace}}';
17-
$routesPath = __DIR__.'/../Http/routes.php';
17+
$pathApi = __DIR__.'/../routes/api.php';
18+
$pathWeb = __DIR__.'/../routes/web.php';
1819

19-
$this->loadRoutesFile($router, $namespace, $routesPath);
20+
$this->loadRoutesFiles($router, $namespace, $pathApi, $pathWeb);
2021
}
2122
}

0 commit comments

Comments
 (0)