Laravel使用用户权限进行路由错误

时间:2014-08-02 22:11:41

标签: php laravel laravel-4 laravel-routing

Laravel的新手,我正在关注一本名为" Laravel 4"我遇到了路由问题。

我的路由文件。

Route::model('cat', 'Cat');

/**
 * Route: Directs all index.php requests to cats index page
 *
 * @Template index.blade.php
 */
Route::get('/', function()
{
    return Redirect::to('cats');
});

/**
 * Route:
 */
Route::get('cats', function()
{
    $cats = Cat::all();
    return View::make('cats.index')
        ->with('cats', $cats);
});

/**
 * Route: Show cats by category.
 *
 * @Template index.php
 */
Route::get('cats/breeds/{name}', function($name)
{
    $breed = Breed::whereName($name)->with('cats')->first();
    return View::make('cats.index')
        ->with('breed', $breed)
        ->with('cats', $breed->cats);
});

/**
 * Route: Directs the user to the login page.
 *
 * @Template login.blade.php
 */
Route::get('login', function()
{
    return View::make('login');
});

/**
 * Route: Directs users to a single cat object page.
 *
 * @Template single.blade.php
 */
Route::get('cats/{cat}', function(Cat $cat)
{
    return View::make('cats.single')
        ->with('cat', $cat);
});

Route::group(array('before'=>'auth'), function()
{

/**
 * Route: Authenticated route to create a cat in the database.
 *
 * @Template edit.blade.php.
 */
Route::get('cats/create', function()
{
    $cat = new Cat;
    return View::make('cats.edit')
        ->with('cat', $cat)
        ->with('method', 'post');
});

/**
 * Route: Edit cat by specific id.
 *
 * @Template edit.blade.php
 */
Route::get('cats/{cat}/edit', function(Cat $cat)
{
    return View::make('cats.edit')
        ->with('cat', $cat)
        ->with("method", 'put');
});

/**
 * Route: Delete a cat from the database
 *
 * @Template edit.blade.php
 */
Route::get('cats/{cat}/delete', function(Cat $cat)
{
    return View::make('cats.edit')
        ->with('cat', $cat)
        ->with('method', 'delete');
});

/**
 * Route: Updates cat object in database then redirects to cat page.
 *
 * @Template None
 */
Route::put('cats/{cat}', function(Cat $cat)
{
    if(Auth::user()->canEdit($cat)) {
        $cat->update(Input::all());
        return Redirect::to('cats/' . $cat->id)
            ->with('message', 'Successfully updated page!');
    }
    else {
        return Redirect::to('cats/' . $cat->id)
            ->with('error', "Unauthorized operation");
    }
});

/**
 *
 */
Route::post('cats', function()
{
    $cat = Cat::create(Input::all());
    $cat->user_id = Auth::user()->id;
    if($cat->save()){
        return Redirect::to('cats/' . $cat->id)
            ->with('message', 'Successfully created profile!');
    }
    else{
        return Redirect::Back()
            ->with('message', 'Could not create profile');
    }
});

/**
 * Route: Deletes cats from database and redirects to cats page
 *
 * @Template: edit.blade.php.
 */
Route::delete('cats/{cat}', function(Cat $cat){
    $cat->delete();
    return Redirect::to('cats')
        ->with('message', 'Successfully deleted page!');
});
});

/**
 * Route: Logs user in or redirects back to previous page.
 *
 * @Template: index.php
 */
Route::post('login', function()
{
    if(Auth::attempt(Input::only('username', 'password'))) {
        return Redirect::intended('/');
    } else {
        return Redirect::back()
            ->withInput()
            ->with('error', "Invalid credentials");
    }
});

/**
 * Route that logs the user out and redirects to index.php.
 */
Route::get('logout', function()
{
    Auth::logout();
    return Redirect::to('/')
        ->with('message', 'You are now logged out');
});

/**
 * Not that sure what this does, Binds cat to a view or something
 */
View::composer('cats.edit', function($view)
{
    $breeds = Breed::all();
    if(count($breeds) > 0){
        $breed_options = array_combine($breeds->lists('id'), $breeds->lists('name'));
    }
    else {
        $breed_options = array(null, 'Unspecified');
    }
    $view->with('breed_options', $breed_options);
});

我正在尝试路由到http://localhost/cats/public/index.php/cats/create并且我一直收到NotFoundHttpException错误。我不确定它是否因为我的溃败的顺序或者是什么。请帮我。

1 个答案:

答案 0 :(得分:1)

您应该测试以下网址:

http://localhost/cats/public/cats/create

而不是:

http://localhost/cats/public/index.php/cats/create

我也认为(我也是Laravel的新手)你应该搬家:

Route::get('cats/create', function()
{
    $cat = new Cat;
    return View::make('cats.edit')
        ->with('cat', $cat)
        ->with('method', 'post');
});

以上

Route::get('cats/{cat}', function(Cat $cat)
{
    return View::make('cats.single')
        ->with('cat', $cat);
});

因为cats/{cat}具有cats/whatever并且如果网址以{{1}}开头

将始终为true