0

My Route:

Route::controller('admin' , 'CategoriesController');

Controller:

<?php

class CategoriesController extends BaseController {

    /**
     * Display a listing of the resource.
     * GET /categories
     *
     * @return Response
     */

    public function __construct() {
        $this->beforeFilter('csrf', array('on' =>'post' ));
    }

    public function getIndex()
    {
        return View::make('categories.index')
        ->with('categories', Category::all());
    }

    /**
     * Show the form for creating a new resource.
     * GET /categories/create
     *
     * @return Response
     */
    public function postCreate()
    {
        $validator = Validator::make(Input::all(), Category::$rules);

        if ($validator->passes()) {
            $category = new Category;
            $category->name = Input::get('name');
            $category->save();

            return Redirect::to('admin/categories/index')
            ->with('message' , 'Category created');
        }

        return Redirect::to('admin/categories/index')
        ->with('message' , 'Something went wrong')
        ->withErrors($validator)
        ->withInput();
    }

    /**
     * Remove the specified resource from storage.
     * DELETE /categories/{id}
     *
     * @param  int  $id
     * @return Response
     */
    public function postDestroy()
    {
        $category = Category::find(Input::get('id'));

        if ($category){
            $category->delete();
            return Redirect::to('admin/categories/index')
            ->with('message' , 'category deleted');
        }
        return Redirect::to('admin/categories/index')
            ->with('message' , 'something went wronng');
    }

}

Model:

<?php

class Category extends \Eloquent {
    protected $fillable = array('name');

    public static $rules = array('name'=>'required|min:3');
}

view/index.blade :

@section('content')
<div id="admin">
    <h1>Categories Admin panel</h1>
    <p>Here you can view, delete, and create new categories.</p>
    <h2>Categories</h2>

    <ul>
    @foreach($categories as $category)
        <li>
        {{ $category->name }} 
        {{ Form::open(array('url'=>'admin/categories/destroy', 'class' => 'form-inline'))}}
        {{ Form::hidden('id' , $category->id) }}
        {{ Form::submit('delete')}}
        {{ Form::close();}}
        </li>
    @endforeach
    </ul>

    <h2>Create New Category</h2>
    @if($errors->has)

        <div id="form-errors">
            <p>The following erros</p>
            <ul>
            @foreach($errors->all() as $error)
                <li>{{ $errors }}</li>
            @endforeach
            </ul>
        </div><!--end form-errors-->
    @endif

    {{ Form::open(array('url'=>'admin/categories/create'))}}
    <p>
    {{ Form::label('name')}}
    {{ Form::text('name')}}
    </p>
    {{ Form::submit('Create Category', array('class' => 'secodary-cart-btn'))}}
    {{ Form::close()}}
</div><!--end admin-->
@stop 

The Problem is: When I run this code in my browser it show undefined variable: category in index.blade.php.

1
  • Do you have stack trace? which controller method you run? Commented Jul 25, 2014 at 21:35

1 Answer 1

2

Please check your route you are trying to access in your browser.

Problem is, you are trying to use RESTful Controllers. In that case, you have to access the URL in the restful method.

If you want to access the getIndex() Method in your restful controller, then in your browser, you have to access

http://www.yoursite.com/admin/index

Basically, Route::controller('admin','CategoriesController'); Means, when you have /admin/ in your URL, it will access this CategoriesController. Then the verbs get followed by the URL path will be used.

If you want to take the non RESTful route, then do it one of the following way

Pass the $categories array like this in your route

Route::get('/', function(){
  return View::make('categories.index')->with('categories',Category::all());
});

OR

Route::get('/',array('uses'=>'CategoriesController@getIndex'));

And then in your CategoriesController

public function getIndex(){
        return View::make('categories.index')
        ->with('categories', Category::all());
}

Should Fix it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.