在laravel 4中使用Route :: resource或Route :: controller

时间:2014-10-15 16:01:53

标签: laravel laravel-4 controller routing laravel-routing

我尝试使用

Route::resource('Feed', 'FeedController');<br/>


Route::controller('Feed', 'FeedController');

两者都不起作用


这是我的路线代码
route.php

Route::get('/', function()
{
    return View::make('pages.home');
});

Route::get('about', function()
{
    return View::make('pages.about');
});

Route::resource('Feed', 'FeedController');

Feed.php

<?php
 class Feed 
   extends Eloquent
 {
protected $fillable = array('feed', 'title', 'active', 'category');
protected $feeds;

public static $form_rules = array(
    'category' => 'required|in:Annoucements,Sports,Events',
    'title'    => 'required',
    'feed'     => 'required',
    'active'   => 'required|between:0,1'
);

public function getParsedFeed($limit = 4)
{
    if (is_null($this->feeds)) {
        $this->feeds = simplexml_load_file($this->feed);
    }

    if (!count($this->feed)) {
        return array();
    }

    $output = array();
    $content = $this->feeds->channel->item;

    foreach (range(0, $limit - 1) as $i) {
        $output[] = $content[$i];
    }

    return $output;
}

}

FeedController.php

<?php

class FeedController extends \BaseController {
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */

    //The method to show the form to add a new feed
    public function create() {
        //We load a view directly and return it to be served
        return View::make('pages.news');
    }

    //Processing the form
    public function store(){
        //Let's first run the validation with all provided input
        $validation = Validator::make(Input::all(),Feeds::$form_rules);
            //If the validation passes, we add the values to the database and
            return to the form
        if($validation->passes()) {
            //We try to insert a new row with Eloquent
            $create = Feeds::create(array('feed'=> Input::get('feed'),
                'title' => Input::get('title'),
                'active' => Input::get('active'),
                'category' => Input::get('category')
            ));

            //We return to the form with success or error message due to state of the

            if($create) {
                return Redirect::to('feeds/create')
                ->with('message','The feed added to the databasesuccessfully!');
            } 
            else {
                return Redirect::to('feeds/create')
                ->withInput()
                ->with('message','The feed could not be added, please try again later!');
            }
        } 
        else {
        //If the validation does not pass, we return to the form with First error message as flash data
            return Redirect::to('feeds/create')
            ->withInput()
            ->with('message',$validation->errors()->first());
        }
    }

}

我需要知道错误的地方我是laravel的新手,我正在通过以下教程来学习它

0 个答案:

没有答案