Support the ongoing development of Laravel.io →
Requests Architecture
Last updated 1 year ago.
0

Hi,

You've created two routes which are the same so you would be redirecting to itself. These routes will match on "/any/any/any", so three parameters in the URL. What you need is a constant string in the url without the braces.

Let me know if that makes sense.

0

You can disambiguate routes with where() but the parameter needs to be a distinct pattern, e.g. text vs digits:

// alphanumeric route
Route::get('{category}/{type}/{name}', 'CategoryController@getByName')->where('name', '[A-Za-z]\w+');

// numeric route
Route::get('{category}/{type}/{id}', 'CategoryController@getById')->where('id', '\d+');

But re-reading your post, I think you want to call a controller from another controller as follows:

// in CategoryController
public function getByName($name)
{
    $app = app();
    $controller = $app->make('ProductController');
    return $controller->callAction('getByName, [$name]);
}

I use this technique in a Wildcard controller I have; it might work for you.

However, controllers should really only be handling requests, and should not contain too much business logic, so you would probably be better off creating a product service or repository class that manages all product-related requests, and call this class from either controller, rather than calling one controller from another.

You may also want to consider if semantically, having one route with different purposes is a good idea. I don't know the difference between a "producentIndex" and an "index" so you would be best-placed to answer that.

Last updated 8 years ago.
0

Thanks for your answer :)

I know about constant string in urls but i need to use variables in urls.

It's online shop page and i need something like this:

i have urls like this: /computers/notebooks/apple /computers/notebooks/acer /computers/pc/hp /accesoriess/notebooks/apple

and with this urls i want to show my client every notebooks from this manufacturers (like a list of products)

but if this pages returns false or 404 (0 products from apple, hp or acer found) i want to use another controller to search and display single product. For example: /computers/notebooks/apple-macbook-pro-13-inch and then return view of product.

0

A page returning 0 products should not 404! It should show 0 results.

And it sounds, as I mentioned, that your controllers are doing too much. This kind of logic should be encapsulated in a Service or Repository class.

The Controller should just be handling the routing.

0

A route without match should show 404.
You can make everything go into a function and decide there if the visitor requests a product or a producer.

Route::get('{category}/{type}/{producent_or_product}', function($c, $t, $p){
$producent = Producents::where('name', $p)->first();
if(!$producent){
// It is a product!
}
else {
// It is a producer!
}
});

Find a way to invoke a controller from the route callback.

0

Ah, producent = producer!

I just had a quick look on some other sites, and I think the easiest way would be to do this:

Route::get('{category}/{type}/{producent}/{product?}', '...');

This way:

  • your route covers both possibilities
  • it's not open to conflicts, such as /apple (computer) vs /fruits/apple
  • it's unambiguous
  • it's logical
  • it's semantic

The route relationship will match the model relationship too (a product can only belong to one producer) and should make it easier and more efficient to build your queries

Hopefully that helps - though I don't know your setup - so it might conflict with some of your other business aims.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

graman graman Joined 29 May 2015

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.