Laravel 4 remove /index on default getIndex controller function

心已入冬 提交于 2019-12-23 12:16:54

问题


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)
    {
        //
    }

}


来源:https://stackoverflow.com/questions/19190389/laravel-4-remove-index-on-default-getindex-controller-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!