4

是否可以在默认的 getIndex restful 控制器功能上删除 /index ?

控制器的定义路由:

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

谢谢

4

2 回答 2

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链接!

于 2014-06-30T05:17:46.817 回答
1

你可以使用喜欢,

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

或者,

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

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

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

在此处阅读文档。

控制器的主要功能将是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)
    {
        //
    }

}
于 2013-10-05T03:35:29.830 回答