【问题标题】:Laravel 4 redirects SlugLaravel 4 重定向 Slug
【发布时间】:2015-03-28 05:24:06
【问题描述】:

我有一条带有 slug 酒店名称的酒店视图路线。

Route::get('/hotel/{slug}', function($slug){
    $id  = Hotel::whereSlug($slug)->firstOrFail()->id;
    $hotel = Hotel::find($id);
    return View::make('hotel')
    ->with('hotel', $hotel);
});

我有一个带有 postStore 方法()的 RoomsController

   <?php

class RoomsController extends BaseController {

public function postStore(){
    $validator = Validator::make(Input::all(), Room::$rules);
    if($validator->passes()){
        $room = new Room;
        $room->name = Input::get('name');
        $room->description = Input::get('description');
        $room->facilities = Input::get('facilities');
        $room->info = Input::get('info');
        $room->price = Input::get('price');
        $room->beds = Input::get('beds');
        $room->no_of_rooms = Input::get('no_of_rooms');
        $room->hotel_id = Input::get('hotel_id');
        $room->save();
        return Redirect::to('hotel/')
        ->with('success', 'Room successfully created!');
    }
    return Redirect::back()
    ->with('error', 'Something went wrong!')
    ->withErrors($validator)
    ->withInput();
  }
}

我的疑问是保存房间后如何重定向到酒店查看页面(上一条路线)。

【问题讨论】:

  • 请附上您拨打postStore()的代码
  • @Rafael 我没听明白我已经发布了有问题的 postStore
  • 你在哪里调用那个函数?
  • 我已经编辑了我的问题.....你现在能帮我吗@Rafael
  • 我真的很想帮助你,但你仍然没有告诉我你在哪里调用 postStore();我的意思是在你的路线中谁正在调用 postStore 方法。是否使用 link_to_action 从视图调用您的方法?你在哪里调用这个函数/调用等等。

标签: redirect laravel laravel-4 laravel-routing


【解决方案1】:

除非有什么你没有告诉我们,否则你有酒店 ID,所以只需加载酒店记录,获取 slug,然后重定向到 url:

// ...
$room->save();
$hotel = Hotel::find(Input::get('hotel_id'));
return Redirect::to(URL::to('hotel', array($hotel->slug)))
    ->with('success', 'Room successfully created!');
// ...

【讨论】:

    猜你喜欢
    • 2013-11-19
    • 2014-11-18
    • 2013-09-03
    • 2013-12-25
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    • 2019-09-04
    • 2015-09-02
    相关资源
    最近更新 更多