我按照this教程设置了一个CRUD示例。但我不能让它工作,我不知道为什么。这是我的代码
SomeitemCrudController.php
<?php
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\SomeitemRequest as StoreRequest;
use App\Http\Requests\SomeitemRequest as UpdateRequest;
class SomeitemCrudController extends CrudController
{
public function setUp()
{
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setModel("App\Models\Someitem");
$this->crud->setRoute("admin/someitem");
$this->crud->setEntityNameStrings('someitem', 'someitems');
/*
|--------------------------------------------------------------------------
| BASIC CRUD INFORMATION
|--------------------------------------------------------------------------
*/
$this->crud->setFromDb();
// $this->crud->setColumns(['nama']);
$this->crud->addField([
'nama' => 'Nama',
'keterangan' => 'Keterangan',
'harga' => 'Harga'
]);
}
public function store(StoreRequest $request)
{
$redirect_location = parent::storeCrud();
return $redirect_location;
}
public function update(UpdateRequest $request)
{
$redirect_location = parent::updateCrud();
return $redirect_location;
}
}
模型,Someitem.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class SomeItem extends Model
{
use CrudTrait;
//
protected $table = 'someitem';
protected $fillable = ['nama', 'keterangan', 'harga'];
public $timestamps = true;
}
请求,SomeitemRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class SomeitemRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return \Auth::check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
// 'name' => 'required|min:5|max:255'
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
然后是路线
<?php
Route::group([
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['admin'],
'namespace' => 'Admin'
], function() {
CRUD::resource('pelayanan', 'Admin\PelayananCrudController');
Route::get('/test', function () {
return view('welcome');
});
});
我可以成功访问http://localhost/myapp/public/admin/test
,但我无法访问http://localhost/myapp/public/admin/someitem
,它始终返回错误500
我是laravel(和PHP)的新手,感谢任何帮助。谢谢!
答案 0 :(得分:1)
我想让我的答案非常详细,这就是为什么我从一开始就开始它的原因。我使用的CRUD背包的版本是^ 3.2。
查看此文件your-project/vendor/backpack/crud/src/CrudServiceProvider.php
是的我知道班级名称不是CRUD
而是CrudServiceProvider
。这是因为他们在register
中使用了CrudServiceProvider
方法,而在$loader->alias('CRUD', \Backpack\CRUD\CrudServiceProvider::class);
中他们CRUD
CrudServiceProvider
使CRUD::resource()
类属于CrudServiceProvider::resource()
。通过阅读Laravel Service Providers,Service Container和Facades了解有关其工作原理的更多信息。
所以我们已经发现return new CrudRouter($name, $controller, $options);
实际上意味着CrudRouter
,所以在这个方法中,你只会找到一行CRUD::resource('pelayanan', 'Admin\PelayananCrudController');
知道(如果我错误地纠正了我),它被称为Factory pattern/method。所以转到Route::post('pelayanan/search', [
'as' => 'crud.pelayanan.search',
'uses' => 'Admin\PelayananCrudController@search',
]);
Route::get('pelayanan/reorder', [
'as' => 'crud.pelayanan.reorder',
'uses' => 'Admin\PelayananCrudController@reorder',
]);
Route::post('pelayanan/reorder', [
'as' => 'crud.pelayanan.save.reorder',
'uses' => 'Admin\PelayananCrudController@saveReorder',
]);
Route::get('pelayanan/{id}/details', [
'as' => 'crud.pelayanan.showDetailsRow',
'uses' => 'Admin\PelayananCrudController@showDetailsRow',
]);
Route::get('pelayanan/{id}/translate/{lang}', [
'as' => 'crud.pelayanan.translateItem',
'uses' => 'Admin\PelayananCrudController@translateItem',
]);
Route::get('pelayanan/{id}/revisions', [
'as' => 'crud.pelayanan.listRevisions',
'uses' => 'Admin\PelayananCrudController@listRevisions',
]);
Route::post('pelayanan/{id}/revisions/{revisionId}/restore', [
'as' => 'crud.pelayanan.restoreRevision',
'uses' => 'Admin\PelayananCrudController@restoreRevision',
]);
构造函数(Github)。仔细阅读。所以它的基本功能是代替这个
public/admin/someitem
它会放置这样的东西
artisan route:list
你说你无法访问`Route::resource($this->name, $this->controller, $options_with_default_route_names);`.
这是真的,因为你没有路由(实际上用index
检查cli中的路由)。但背包开发者做了一件事(实际上并不知道为什么)他们通过调用
php artisan vendor:publish --provider="Backpack\Base\BaseServiceProvider"
实际应该可行,如果不是,请检查CGFloat xScale = self.view.transform.a;
CGFloat yScale = self.view.transform.d;
(CrudController)的行为。
还有一件事可能是你忘了{{1}}而你没有查看文件,这就是你得到500错误的原因。
如果您有更多问题,请询问。