Support the ongoing development of Laravel.io →
posted 8 years ago
Architecture
Last updated 2 years ago.
0

Just

Route::get('/categories/{id}/{format}', ['uses'=>'CategoryController@categoryById']);
0

@voyowsky yeah but i dont want the format in the url, thats why i gave the example of the two diferent routes ;)

0

thats why the title is "Passing parameters outside url to shorthand routes" ;)

0

edited my post with that info ;)

0

I'm sorry @BartHuis. My bad. :)

Is there a reason to why you want to do this?

I can only think of one way to do this:

Route::get('/', [function (Illuminate\Http\Request $request) {
    $controller = new \App\Http\Controllers\SomeController();
    return $controller->getAction($request, 'my format');
}]);

Sorry if i wasted your time. This probably isn't what you've been looking for.

Last updated 8 years ago.
0

haha thanks @voyowsky that was the alternative i thought about. but it looks better in my routes file if i could handle it by shorthand routes. thats why i wanted to know if its possible to pass parameters to the function without adding it to the url path... ;)

Last updated 8 years ago.
0

@voyowsky dont know why you included the iluminate request in and let the function in an array, but this edited one from your suggestion is indeed working:

    Route::group(['prefix' => '/something'], function() {
        Route::get('/categories/{id}', function ($id) {
            $controller = new \App\Http\Controllers\CategoryController();
            return $controller->categoryById($id, 'json');
        }); // returns given category as json
    }
    Route::group(['prefix' => '/categories'], function() {
        Route::get('/{id}', function ($id) {
            $controller = new \App\Http\Controllers\CategoryController();
            return $controller->categoryById($id, 'view');
        }); // returns given category as view); // returns given category as view
    });

but it would be nice if someone still can come up with a shorthand method to do this, this way it would almost be more efficient to put everything from the controller function directly to this function, while my intension is to put as much as possible outside the controllers :)

0

Oh, i put the request there in case you needed to use additional parameters in the controller, without using the request helper, like so:

public function categoryById (Request $request, $id, $format) {
        $category = \App\Category::with('product')->findOrFail($id);
        if($format == 'json'){
            return response()->json($category);
        }
        else if($format == 'view'){
            return viewForDomain('category', ['category'=>$category]);
        }
        $request->input('another_query_or_form_parameter');
        // instead of using request()->input('another_param');
}

Normally, the request would have been injected into the controller method/constructor, but in your case, you need to add it yourself.

About the array, i just forgot to remove it. It is redundant, so just ignore it. :)

Last updated 8 years ago.
0

ah, maybe in the future i'll need and add it ;) i'm not there yet :P

0

for who's interested, it was to make the api part of my app, at laracasts forum there more discussion on this part and how to handle that in other ways ;)

https://laracasts.com/discuss/channels/laravel/passing-parameters-outside-url-to-shorthand-routes

0

Sign in to participate in this thread!

Eventy

Your banner here too?

BartHuis barthuis Joined 19 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.