简体   繁体   中英

How to route to a laravel controllers method

I'm quite new to laravel4 but with some codeigniter background. I try to figure out how do I route with url to a controller method

my url should look like

/admin/products{controller_name}/parser{controller_method}

than controller

<?php namespace App\Controllers\Admin;

use App\Models\Product;
use Image, Input, Notification, Redirect, Sentry, Str;

    class ProductsController extends \BaseController {

        public function index()
        {
            return \View::make('admin.products.index');
        }

            public function parser()
        {
            return \View::make('admin.products.parser');
        }

    }


Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function()
{

        Route::resource('products',           'App\Controllers\Admin\ProductsController');
        Route::resource('products/parser',    'App\Controllers\Admin\ProductsController@parser');

});

When you use the Route::resource method, you're actually creating many different routes in a single call:

  1. GET /admin/products
    • maps to an index method on the controller
  2. GET /admin/products/create
    • maps to a create method on the controller
  3. POST /admin/products
    • maps to a store method on the controller
  4. GET /admin/products/{id}
    • maps to a show method on the controller
  5. GET /admin/products/{id}/edit
    • maps to an edit method on the controller
  6. PUT/PATCH /admin/products/{id}
    • maps to an update method on the controller
  7. DELETE /admin/products/{id}
    • maps to a destroy method on the controller

Saying Route::resource('resource', 'Controller') is all that you would need to do in order to create 7 different routes, and is a convenience method really helpful when creating API's.

All that said, I don't think this is what you want to do. Instead, I think you just want to use the regular get and/or post methods for this:

// Here is a single GET route
Route::get('products', 'App\Controllers\Admin\ProductsController@index');
// Here is a single POST route
Route::post('products/parser', 'App\Controllers\Admin\ProductsController@parser');

See more info on Laravel's Resource Controllers in the docs .

As a side note, you can see all of the routes your application is currently aware of by using Artisan's routes command:

$ php artisan routes

You can verify a route is setup correctly by running that command and looking for what method on what controller a given route is being mapped to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM