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

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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错