Search code examples
phppostlaravel-4slug

Laravel post with slug


I want to execute an post method, but that doesn't seem to work...

So I have right now the URL localhost/newthread/5.

That's all fine, but now I want to have that the 5 (slug) can be posted.

How Can I do this?

View:

@include('globs.header')

<div class="panel-group col-sm-offset-1 col-sm-10">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Nieuw topic maken</h3>
      </div>
      <div class="panel-body">
        <!-- Start Form -->
        {{ Form::open(array('url' => 'PostTopic')) }}
          <div class="form-group">
            <label>Topic Naam (onderwerp):</label>
            <input type="text" class="form-control" name="title">
          </div>

            <label>Bericht:</label>
            <textarea name="message" class="form-control" rows="5" placeholder="Typ uw bericht..."></textarea>

            <br>
          <button type="submit" class="btn btn-success">Nieuw topic plaatsen</button>

        {{ Form::close() }}
      </div>
    </div>
</div><!-- End page content -->

@include('globs.footer')

Controller:

public function PostTopic()
    {
        //Get all the data and store it inside Store Variable

        $data = Input::all();

        // Make's messages of faults
        $messages = array(
           'title.required'     => 'U moet een titel opgeven!',
           'message.required'   => 'u moet een bericht opgeven!',
           'spamprt'            => 'honeypot', //spam protection
           'time'               => 'required|honeytime:60'
        );

        $rules = array(
            'title' => 'required',
            'message' => 'required'
            );

        $validator = Validator::make($data, $rules, $messages);

        //process the storage
        if ($validator->fails())
        {
            return Redirect::back()->with('errors', $validator->errors())->withInput();
        }else{

            //store
            $thread                     = new Thread;
            $thread->cid                = Input::get('cid');
            $thread->title              = Input::get('title');
            $thread->message            = Input::get('message');
            $thread->prefix             = 0;
            $thread->uid                = Auth::id();
            $thread->username           = Auth::user()->username;
            $thread->date_posted        = Carbon\Carbon::now();
            $thread->save();
            Session::put('_token', sha1(microtime()));
            //redirect
            return Redirect::back()->with('message', 'Uw bericht is succesvol geplaatst!');
            }
    }

Routes:

Route::group(array('before' => 'auth'), function()
{
    Route::get('/newthread/{cid}', array('uses' => 'ForumController@CreateTopic', 'as' => 'Nieuw topic'));
});
Route::post('/PostTopic', array('uses' => 'ForumController@PostTopic', 'as' => 'Post_topic'));

It is strange, An hidden input is something I can't do. Because as you see in my view, it is just a view. How should I fix this?


Solution

  • Try to use post {cid} as a query parameter

    public function CreateTopic($cid)
        {
            $data['cid']=$cid;
            return View::make('sayfa.createtopic',$data);
        }
    

    Part of View:

    {{ Form::open(array('url' => 'PostTopic/'.$cid)) }}
                    <div class="form-group">
                        <label>Topic Naam (onderwerp):</label>
                        <input type="text" class="form-control" name="title">
                    </div>
    
                    <label>Bericht:</label>
                    <textarea name="message" class="form-control" rows="5" placeholder="Typ uw bericht..."></textarea>
    
                    <br>
                    <button type="submit" class="btn btn-success">Nieuw topic plaatsen</button>
    
                    {{ Form::close() }}
    

    route:

    Route::post('/PostTopic/{cid}', array('uses' => 'SOController@PostTopic', 'as' => 'Post_topic'));
    

    Post method:

    public function PostTopic($cid){
           //you can now have $cid with other inputs
        }