Create dynamic routes in Laravel - laravel

I'm working on ecommerce website, Stuck in nav bar, I have created routes something like this:
Route::get('/category/{slug}', 'Site\CategoryController#show')->name('category.show');
In slug i pass slug of Product,
but i want to change something like this
Route::get('/{slug}/{slug}', 'Site\CategoryController#show')->name('category.show');
i want to remove category prefix and pass main category slug as a first parameter and sub category if it exist as a second parameter otherwise it will be empty.
One more thing i am using TypiCMS, for creating Nestable menu and it is working will i have to modify that also to work with the dynamic route.

Sorry I don't know anything about your cms framework but in Laravel you cant use the same name for two bindings in your route, they must each be unique.
Route::get('/{categoyrySlug}/{subcategorySlug}', 'Site\CategoryController#show')->name('category.show');
And in Site\CategoryController you should be able to use :
public function show($categorySlug, $subcategorySlug){
...
}
and then handle them accodingly.

Related

How to differentiate between two dynamic url in Laravel

I have two dynamic url with simillar structure. For example, lets say, Product page and category page.
I have set both pages in
Route::get('/{product}', [UsersController:: class, 'productDetail']);
Route::get('/{category}', [UsersController:: class, 'categoryProducts']);
But when I click on url which suppose to go in category page, it redirect to product page only because of same structure. How I can differentiate both URLs for Laravel without altering their url structure?
I don't think this can be done without modifying the URL pattern at least a little bit.
If you do something like /50?type=category then in the show method you can use the query parameter to determine which table to look at. But you'll have to use the same show method and I don't recommend doing it this way.
I hope someone else will be able to shine some more light on the matter.
this is the best practice for your case to make yourapi Resful
Route::get('/product/{product-id}', [UsersController:: class, 'productDetail']);
Route::get('/product/categories, [UsersController:: class, 'categoryProducts']);
learn more about Restful api here https://restfulapi.net/resource-naming/
This should be done by calling index, update diff() function. You can try by using the below:
Route::get('/category/{slug}', 'site\categorycontroller#show')->name('category.show');
Route::get('/product/{slug}', 'site\productcontroller#show')->name('product.show');

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

Using multiple Routes and Controllers on single blade file

I have created multiple controller and routes but they are working 1 at a time, I have to disable the other and change the code of my blade file or use different blade file for them but is there an easy way to use it.
The routes are
Route::get('/students/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/{name}', 'PostController#showByName');
Route::get('/students/{class}', 'PostController#showByClass');
I do not want to create different blade files like
http://example.com/students/alphabet/a
http://example.com/students/name/nadia
http://example.com/students/class/b_com
but like this
http://example.com/students/a
http://example.com/students/nadia
http://example.com/students/b_com
is it possible?
All controllers show different data.
1. Alphabet show list of students starting with same initial.
2. Name shows profile data of the student.
3. Class shows list of students in that subject class.
Since you have a wildcard at the end of your routes the first one will always trigger. So make sure you have individual routes for the controller functions. You can still use the same blade file in the controller.
Route::get('/students/alphabet/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/name/{name}', 'PostController#showByName');
Route::get('/students/class/{class}', 'PostController#showByClass');
If you have the same route with a parameter there is no way for the router to know if the characters you're sending in are alphabet, name or class.

Routing to Controller in Laravel4

I am using laravel for the first time and need some help understanding routes. I am setting up a view that will show a list of orders placed by customers. Above this list are 2 search boxes. One for searching by ID, the other for selecting a date. I'd like to have a "default" route so when id/date are NOT included in the route, we see all orders placed so far today.
Routes should be as follows:
orders - Should display all orders placed today.
orders/{id} - Should show only the specific order that belongs to that id.
orders/{date} -
Should show all orders placed on a specific date.
{id} and {date} should be distinguished by regular expressions.
I can get any one of the routes to work by themselves but when I try to create the routes for all 3 and modify my controller accordingly I break the others. One example is this:
Route::get('orders/{id}', 'OrderController#getOrders')->where('id', '[0-9]+');
Which works for getting the order by ID, but if I wanted to allow for dates as well I would have to change the route entirely. From that point I'd like to be able to go even further, for example the route orders/12345/edit should bring me to the view that allows me to edit the order.
How can I properly setup my routes/controller to do this?
Unless you manage to write a regular expression that validates dates or numeric values you have two options:
Write two different routes: one that validates dates and other that validates IDs. Both would point to different methods in the controller.
Use one route that doesn't validate its the parameter and that points to one method in the controller where the type of parameter would be checked for date or ID.
I like the first option better, because I believe both routes are similar yet very different.
EDIT
If you want to use the same form to target to different urls depending on the contents of inputs you have to use javascript, you can change the action in the form using:
$('#form').attr('action', "the_url");
And you'd have to set up a listener for the inputs to know which url to point to:
Detecting input change in jQuery?
I hope this helps you!
just make three routes like laravel documentation
orders route:
Route::get('orders', 'OrderController#getOrders');
orders by id route:
Route::get('orders/{id}','OrderController#getOrdersById')->where('id', '[0-9]+');
orders by data route:
Route::get('orders/{data}', 'OrderController#getOrdersByData')->where('name', '[A-Za-z]+');
also you can create three route into your OrderController like documentation

friendly url in codeigniter with variables in url

I'm making a site using Codeigniter and my URL for a particular product page is like http://www.domain.com/products/display/$category_id/$product_id/$offset
$offset is used for limiting the number of pages shown per page when using the Codeigniter's Pagination library.
How I want to make it such that my URL is something more human friendly, like http://www.domain.com/$category_name/$product_name/$offset ie. http://www.domain.com/weapons/proton-canon/3
Can anyone point me the general approach? I just started learning codeigniter and is working on my first project
You can use what's generally known as a URL slug to achieve this.
Add a new field to your table, called "url_slug" or similar. Now you will need to create a slug for each product, and store it in this field.
CI has a function in the URL helper - url_title($string) which will take a string, and convert it for use in URL's.
For example My product name would become my_product_name.
Now, in your method, you can either - keep the product_id intact, use this as a parameter for your method to show specific products, and use the slug for human friendly links, or you can just use the url_slug to refer to products.
Your URL may look like:
www.domain.com/$category_name/$product_id/my_cool_product/$offset
or it could look like
www.domain.com/$category_name/my_cool_product/$offset
with no ID. the choice is yours, but the url_slug may change - the ID won't. Which may have SEO impacts.
Regardless, your method needs to look something like:
function display_product($product_id, $url_slug, $offset) {
// do what you gotta do...
}
You can then use URL's like the above.
You will need to use URI routing as well, as the example above will attempt to look for a controller called $category_name and a method called my_cool_product, which will of course not exist.
See URI Routing for further info.

Resources