Laravel 4删除/索引默认的getIndex控制器功能

时间:2013-10-04 20:39:32

标签: laravel laravel-4 blade laravel-routing

是否可以删除/索引默认的getIndex restful controller函数?

控制器的定义路线:

Route::controller('registration', 'RegisterController', array(
  'getIndex' => 'getRegister'
)); 

控制器:

class RegisterController extends UserController {

  public function getIndex()
  {
    // Show the register page
    return View::make('register');
  }
}

例如,在我的login.blade.php中,我有:

{{ HTML::link(URL::route('getRegister'), 'New User?', array('title' => 'Novi korisnik?', 'class'  => 'wideBtn', 'id' => 'userRegisterLink')) }}

并返回结果是这样的链接:http://mydomain.com/registration/index

我更喜欢使用路由名称获取URL :: route()上的链接URL,并且我希望返回的链接很简单,如下所示:http://mydomain.com/registration

由于

2 个答案:

答案 0 :(得分:3)

在routes.php文件中:

<?php

Route::get('/one', 'OneController@getIndex');
Route::get('/two', 'TwoController@getIndex');
Route::get('/', 'HomeController@getIndex');

Route::controller('/one', 'OneController');
Route::controller('/two', 'TwoController');
Route::controller('/', 'HomeController');

在任何视图中:

{{ action('HomeController@getIndex') }} will now return http://example.com/
{{ action('OneController@getIndex') }} will now return http://example.com/one
{{ action('TwoController@getIndex') }} will now return http://example.com/two

其余的控制器方法仍然映射相同。只要确保他们获得路线(或者如果他们需要任何/后期方法)。并且它们在控制器方法映射调用之前。不再有http://example.com/index链接了!

答案 1 :(得分:1)

你可以使用喜欢,

Route::resource('registration', 'RegisterController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));

或者,

Route::resource('registration', 'RegisterController');

然后,您可以通过index访问GET http://localhost/laravel/registration

{{ HTML::link(URL::to('registration'), 'New User?', array('title' => 'Novi korisnik?', 'class'  => 'wideBtn', 'id' => 'userRegisterLink')) }}

阅读文档here

控制器主要功能为index, store, show, update, destroy

<?php

class RegistrationController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     * GET http://localhost/laravel/registration
     */
    public function index()
    {
        return View::make('registrations.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('registrations.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     *  POST http://localhost/laravel/registration
     */
    public function store()
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     * GET http://localhost/laravel/registration/1
     */
    public function show($id)
    {
        return View::make('registrations.show');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        return View::make('registrations.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     * PUT http://localhost/laravel/registration/1
     */
    public function update($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     * DELETE http://localhost/laravel/registration/1
     */
    public function destroy($id)
    {
        //
    }

}