繁体   English   中英

具有用户权限的Laravel路由错误

[英]Laravel Routing errors with user permissions

Laravel和im的新手,以及一本名为《 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错误的目录。 我不确定这是因为我的溃败顺序还是什么。 请帮我。

您应该测试以下网址:

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/whatever what开头,则带有cats/{cat}那个将始终为true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM