Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to route to a laravel controllers method

Tags:

php

laravel-4

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

});
like image 565
fefe Avatar asked Dec 20 '25 23:12

fefe


1 Answers

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.

like image 79
Jeff Lambert Avatar answered Dec 23 '25 12:12

Jeff Lambert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!