dounieliang4712 2014-08-02 22:11
浏览 48
已采纳

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

New to Laravel and im following along with a book called "Getting Started with Laravel 4" and i ran into a problem with routing.

My Routing file.

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);
});

I am trying to route to http://localhost/cats/public/index.php/cats/create an d i keep getting the NotFoundHttpException error. I not sure if its because of the order of my routs or what. Please help me.

  • 写回答

1条回答 默认 最新

  • dongtang2376 2014-08-02 22:22
    关注

    You should rather test the following url:

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

    and not:

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

    I also think (I'm new to Laravel too) that you should move:

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

    above

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

    because the one with cats/{cat} and will be always true if url starts with cats/whatever

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行