Laravel 4 remove /index on default getIndex contro

2019-06-24 08:48发布

问题:

Is it possible to remove /index on default getIndex restful controller function?

Defined route for controller:

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

Controller:

class RegisterController extends UserController {

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

For example, in my login.blade.php i have:

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

and returned result is link like this: http://mydomain.com/registration/index

I prefer to get link URL over URL::route() with route name, and i want returned link to be simple as this: http://mydomain.com/registration

Thanks

回答1:

In your routes.php file:

<?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');

In any view:

{{ 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

And the rest of the controller methods are still mapped the same. Just make sure they are get routes (or if they need to be any/post methods). And that they are before the controller method mapping call. No more http://example.com/index links anymore!



回答2:

You can use like,

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

Or,

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

Then you can access index by GET http://localhost/laravel/registration like,

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

Read documentation here.

The controller main functions will be 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)
    {
        //
    }

}