Маршруты — у меня есть 2 маршрута, связанные с этим
Route :: resource (‘items’, ‘ItemsController’);
Route :: get (‘process / {process} / items’, ‘ItemsController @ index’);
Когда я использую 2-й, функция индекса (в контроллере, упомянутом выше) выбирает идентификатор процесса и работает без помех.
Это ссылка в отдельном представлении, которое использует 2-й маршрут, указанный выше:
{{HTML :: link (‘process /’ .$ process-> id. ‘/ Items’, ‘Manage Items’, массив (‘class’ => ‘btn btn-primary’))}}
Когда я использую перенаправление из функции обновления в том же контроллере, я получаю
«Отсутствует аргумент 1 для ItemsController :: index ()»
это функция, которая принимает параметр, чтобы он мог отображать все элементы с этим идентификатором.
Кажется, не имеет значения, что я использую. Вот некоторые из утверждений, которые я пробовал для перенаправления на функцию индекса:
return Redirect :: route (‘items.index’, массив ($ data [‘process_id’]));
return Redirect :: action (‘ItemsController @ index’, массив ($ data [‘process_id’]));
Я также пытался использовать «с (…)»
Следующее (с использованием либо маршрута, либо действия) выдает ошибку «маршрут не определен»:
return Redirect :: action (‘process /’ .$ data [‘process_id’]. ‘/ items’);
Я не думаю, что это хорошая практика, чтобы воссоздать представление, как в функции индекса. Я должен просто быть в состоянии перенаправить и покончил с этим.
Что я делаю неправильно?
Типовые отношения следующие:
1 проект имеет множество предметов
1 пункт имеет множество атрибутов
1 атрибут имеет множество расширений
<?php
class ItemAttributesController extends \BaseController {
/**
* Display a listing of itemattributes
*
* @return Response
*/
public function index($item_id)
{
return $this->makeIndex($item_id);
}
/**
* Show the form for creating a new itemattribute
*
* @return Response
*/
public function create()
{
return View::make('item_attributes.create');
}
/**
* Store a newly created itemattribute in storage.
*
* @return Response
*/
public function store()
{
$validator = Validator::make($data = Input::all(), Itemattribute::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$attribute = Itemattribute::create($data);
// add created attribute id to sequence in the item for this attribute
$item = Item::findOrFail($attribute->item_id);
// get the sequence data
// append the attribute id
if (isset($item->attribute)){
$item->attribute = $item->attribute.', '.$attribute->id;
} else {
$item->attribute = $attribute->id;
}
$item->save();
return $this->makeIndex($data['item_id']);
}
/**
* Display the specified itemattribute.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$itemattribute = Itemattribute::findOrFail($id);
return View::make('item_attributes.show', compact('itemattribute'));
}
/**
* Show the form for editing the specified itemattribute.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$itemattribute = Itemattribute::find($id);
return View::make('item_attributes.edit', compact('itemattribute'));
}
/**
* Update the specified itemattribute in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$itemattribute = Itemattribute::findOrFail($id);
$validator = Validator::make($data = Input::all(), Itemattribute::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$itemattribute->update($data);
return $this->makeIndex($data['item_id']);
}
/**
* Remove the specified itemattribute from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$attribute = Itemattribute::findOrFail($id);
//find the item
$item = Item::findOrFail($attribute->item_id);
// get the sequence string
if (isset($item->attribute)){
// convert to array
$arr=explode(",",$item->attribute);
// remove item
if(($key = array_search($id, $arr)) !== false) {
unset($arr[$key]);
}
// convert back to string and replace initial string
$item->attribute = implode(",", $arr);
// save
$item->save();
}
ItemAttribute::destroy($id);
// return Redirect::route('item_attributes.index');
return $this->makeIndex($attribute->item_id);
}
private function makeIndex($item_id){
$item = Item::findOrFail($item_id);
$project = Project::findOrFail($item->project_id);
$item_attributes = DB::table('item_attributes')->where('item_id', $item->id)->get();
return View::make('item_attributes.index', compact('item_attributes', 'item', 'project'));
// return Redirect::to('item_attributes', );
}
}
Route::get('/', function()
{
return View::make('home');
});
Route::resource('projects', 'ProjectsController');
Route::resource('items', 'ItemsController');
Route::resource('item_attributes', 'ItemAttributesController');
Route::resource('attribute_extentions', 'AttributeExtensionsController');Route::get('project/{projects}/items', 'ItemsController@index');
Route::get('project/{projects}/item/create', 'ItemsController@create');
Route::get('item/{items}/item_attributes', array('as' => 'item_attributes', 'uses' => 'ItemAttributesController@index'));
Route::get('item/{items}/attribute_extentions', 'AttributeExtensionsController@index');
Вы не используете действие маршрута правильно, оно должно указывать на функцию controller @.
return Redirect::action('ItemsController@index', array('item_id' => 1));
Починил это!
Решение было использовать
return Redirect::to('item/'.$item->id.'/item_attributes');
Это правильно передало параметр таким образом, чтобы он мог быть выбран функцией в качестве параметра.
Спасибо за вашу помощь!