Skip to content

Commit 74b6878

Browse files
committed
Update controller command (simplified resource stub, unless admin specified)
1 parent 69641f0 commit 74b6878

9 files changed

+124
-43
lines changed

readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Custom Laravel 5 File Generators with a config file and publishable stubs.
44
You can publish the stubs. You can add your own stubs to generate.
55

6-
Interested in a laravel admin starter project where the package is being used. [Admin Starter Project](https://github.com/bpocallaghan/laravel-admin-starter)
6+
This package is being used in the [Admin Starter Project](https://github.com/bpocallaghan/laravel-admin-starter)
7+
(Page Builder, Roles, Impersonation, Analytics, Blog, News, Banners, FAQ, Testimonials and more)
78

89
```
910
Laravel 5.1 - v2.1.3

resources/stubs/controller.plain.stub

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace {{namespace}};
44

55
use Illuminate\Http\Request;
6-
76
use {{rootNamespace}}Http\Requests;
87
use {{rootNamespace}}Http\Controllers\Controller;
98

resources/stubs/controller.stub

+9-32
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,74 @@
22

33
namespace {{namespace}};
44

5-
use Redirect;
65
use {{rootNamespace}}Http\Requests;
76
use {{rootNamespace}}Models\{{model}};
87
use Illuminate\Http\Request;
9-
use {{rootNamespace}}Http\Controllers\Admin\AdminController;
8+
use {{rootNamespace}}Http\Controllers\Controller;
109

11-
class {{class}} extends AdminController
10+
class {{class}} extends Controller
1211
{
1312
/**
1413
* Display a listing of {{resource}}.
15-
*
16-
* @return Response
1714
*/
1815
public function index()
1916
{
20-
save_resource_url();
21-
22-
return $this->view('{{viewPath}}.index')->with('items', {{model}}::all());
17+
//
2318
}
2419

2520
/**
2621
* Show the form for creating a new {{resource}}.
27-
*
28-
* @return Response
2922
*/
3023
public function create()
3124
{
32-
return $this->view('{{viewPath}}.create_edit');
25+
//
3326
}
3427

3528
/**
3629
* Store a newly created {{resource}} in storage.
37-
*
38-
* @return Response
3930
*/
4031
public function store()
4132
{
42-
$attributes = request()->validate({{model}}::$rules, {{model}}::$messages);
43-
44-
${{resource}} = $this->createEntry({{model}}::class, $attributes);
45-
46-
return redirect_to_resource();
33+
//
4734
}
4835

4936
/**
5037
* Display the specified {{resource}}.
5138
*
5239
* @param {{model}} ${{resource}}
53-
* @return Response
5440
*/
5541
public function show({{model}} ${{resource}})
5642
{
57-
return $this->view('{{viewPath}}.show')->with('item', ${{resource}});
43+
//
5844
}
5945

6046
/**
6147
* Show the form for editing the specified {{resource}}.
6248
*
6349
* @param {{model}} ${{resource}}
64-
* @return Response
6550
*/
6651
public function edit({{model}} ${{resource}})
6752
{
68-
return $this->view('{{viewPath}}.create_edit')->with('item', ${{resource}});
53+
//
6954
}
7055

7156
/**
7257
* Update the specified {{resource}} in storage.
7358
*
7459
* @param {{model}} ${{resource}}
75-
* @return Response
7660
*/
7761
public function update({{model}} ${{resource}})
7862
{
79-
$attributes = request()->validate({{model}}::$rules, {{model}}::$messages);
80-
81-
${{resource}} = $this->updateEntry(${{resource}}, $attributes);
82-
83-
return redirect_to_resource();
63+
//
8464
}
8565

8666
/**
8767
* Remove the specified {{resource}} from storage.
8868
*
8969
* @param {{model}} ${{resource}}
90-
* @return Response
9170
*/
9271
public function destroy({{model}} ${{resource}})
9372
{
94-
$this->deleteEntry(${{resource}}, request());
95-
96-
return redirect_to_resource();
73+
//
9774
}
9875
}

resources/stubs/controller_admin.stub

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace {{namespace}};
4+
5+
use {{rootNamespace}}Http\Requests;
6+
use {{rootNamespace}}Models\{{model}};
7+
use Illuminate\Http\Request;
8+
use {{rootNamespace}}Http\Controllers\Admin\AdminController;
9+
10+
class {{class}} extends AdminController
11+
{
12+
/**
13+
* Display a listing of {{resource}}.
14+
*
15+
* @return $this
16+
*/
17+
public function index()
18+
{
19+
save_resource_url();
20+
21+
return $this->view('{{viewPath}}.index')->with('items', {{model}}::all());
22+
}
23+
24+
/**
25+
* Show the form for creating a new {{resource}}.
26+
*
27+
* @return $this
28+
*/
29+
public function create()
30+
{
31+
return $this->view('{{viewPath}}.create_edit');
32+
}
33+
34+
/**
35+
* Store a newly created {{resource}} in storage.
36+
*
37+
* @return $this
38+
*/
39+
public function store()
40+
{
41+
$attributes = request()->validate({{model}}::$rules, {{model}}::$messages);
42+
43+
${{resource}} = $this->createEntry({{model}}::class, $attributes);
44+
45+
return redirect_to_resource();
46+
}
47+
48+
/**
49+
* Display the specified {{resource}}.
50+
*
51+
* @param {{model}} ${{resource}}
52+
* @return $this
53+
*/
54+
public function show({{model}} ${{resource}})
55+
{
56+
return $this->view('{{viewPath}}.show')->with('item', ${{resource}});
57+
}
58+
59+
/**
60+
* Show the form for editing the specified {{resource}}.
61+
*
62+
* @param {{model}} ${{resource}}
63+
* @return $this
64+
*/
65+
public function edit({{model}} ${{resource}})
66+
{
67+
return $this->view('{{viewPath}}.create_edit')->with('item', ${{resource}});
68+
}
69+
70+
/**
71+
* Update the specified {{resource}} in storage.
72+
*
73+
* @param {{model}} ${{resource}}
74+
* @return $this
75+
*/
76+
public function update({{model}} ${{resource}})
77+
{
78+
$attributes = request()->validate({{model}}::$rules, {{model}}::$messages);
79+
80+
${{resource}} = $this->updateEntry(${{resource}}, $attributes);
81+
82+
return redirect_to_resource();
83+
}
84+
85+
/**
86+
* Remove the specified {{resource}} from storage.
87+
*
88+
* @param {{model}} ${{resource}}
89+
* @return $this
90+
*/
91+
public function destroy({{model}} ${{resource}})
92+
{
93+
$this->deleteEntry(${{resource}}, request());
94+
95+
return redirect_to_resource();
96+
}
97+
}

src/Commands/GeneratorCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
namespace Bpocallaghan\Generators\Commands;
44

5-
use Bpocallaghan\Generators\Traits\ArgumentsOptionsAccessors;
6-
use Bpocallaghan\Generators\Traits\SettingsAccessors;
7-
use Illuminate\Filesystem\Filesystem;
85
use Illuminate\Support\Composer;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Bpocallaghan\Generators\Traits\Settings;
98
use Symfony\Component\Console\Input\InputOption;
109
use Symfony\Component\Console\Input\InputArgument;
10+
use Bpocallaghan\Generators\Traits\ArgumentsOptions;
1111
use Illuminate\Console\GeneratorCommand as LaravelGeneratorCommand;
1212

1313
abstract class GeneratorCommand extends LaravelGeneratorCommand
1414
{
15-
use ArgumentsOptionsAccessors, SettingsAccessors;
15+
use ArgumentsOptions, Settings;
1616

1717
/**
1818
* @var Composer

src/Commands/ResourceCommand.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,17 @@ private function callController()
128128
$name = substr_replace($arg, str_plural($this->resource),
129129
strrpos($arg, $this->resource), strlen($this->resource));
130130

131-
if (!$this->repositoryContract) {
132-
$this->callCommandFile('controller', $name);
131+
if ($this->repositoryContract) {
132+
$this->callCommandFile('controller', $name, 'controller_repository');
133133
}
134134
else {
135-
$this->callCommandFile('controller', $name, 'controller_repository');
135+
// if admin - update stub
136+
if (!str_contains($name, 'admin.')) {
137+
$this->callCommandFile('controller', $name, 'controller');
138+
}
139+
else {
140+
$this->callCommandFile('controller', $name, 'controller_admin');
141+
}
136142
}
137143
}
138144
}

src/Traits/ArgumentsOptionsAccessors.php renamed to src/Traits/ArgumentsOptions.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bpocallaghan\Generators\Traits;
44

5-
trait ArgumentsOptionsAccessors
5+
trait ArgumentsOptions
66
{
77
/**
88
* Get the argument name of the file that needs to be generated

src/Traits/SettingsAccessors.php renamed to src/Traits/Settings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Bpocallaghan\Generators\Traits;
44

5-
trait SettingsAccessors
5+
trait Settings
66
{
77
/**
88
* Settings of the file type to be generated

src/config/config.php

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
'migration_plain_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/migration.plain.stub',
128128
'controller_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/controller.stub',
129129
'controller_plain_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/controller.plain.stub',
130+
'controller_admin_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/controller_admin.stub',
130131
'controller_repository_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/controller_repository.stub',
131132
'pivot_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/pivot.stub',
132133
'seed_stub' => base_path() . '/vendor/bpocallaghan/generators/resources/stubs/seed.stub',

0 commit comments

Comments
 (0)