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.
Related
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.
I am using Laravel 5.2.
I am passing parameters via two urls. But it always goes to the first route only.
code in my routes.php is as follows:
Route::get('{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('{id}', 'settings#load_staff')->middleware('auth');
Anchor tags in view is as follows:
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>
The issue is it always uses 'settings#load_department_settings'. How could i use 'settings#load_staff' when clicking on staff name?
It is always going to use the {department} route because that is matching anything on the root / slash. You need tto dive at least one of them unique prefixes. For example:
Route::get('department/{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('staff/{id}', 'settings#load_staff')->middleware('auth');
Then
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>
I have the following URL example.com/post with this route Route::get('/post','PostController#index). Furthermore, I have another route and contoller pair: example.com/comment and Route::get('/comment','CommentController#index').
Each path is independently navigatable. Now in my index, I would like to combine both of these two together to show comments and posts together.
What should I do?
1) Make the pair of Route::get('\','IndexController#index') and combine two controllers, that is first echo $commentController->index() and then return $postController->index().
2)Make a separate view for index and pass it the post and comment data.
3)Neither of these methods are proper and I should do something totally different.
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
I am trying to implement multi-language URLs. Thus I want to have URLs like:
/de/ueber-uns/kontakt and /en/about-us/contact
So far so good, I use App::before() in filters.php to check the locale given. I think I then need a route in routes.php for every controller action in every language.
So I thought of dynamically creating the file routes.php. All I would need for it is to know how I can access all available controllers or get all registered routes in code (like artisan routes but not with CLI).
So the questions are:
is the general approach for multilingual urls correct?
is it possible to access all controllers to extract the methods somehow?
how could I get the RouteCollection that is used within \Illuminate\Routing\Router.php?
Thank you in advance!
I ended up doing the following:
1) Routes in routes.php are dynamically created with a custom artisan command. It parses all Controllers and creates routes for every action in every language that is supported. The language string is handled with routes like
Route::get('{lang}/customer/login', 'CustomerController#getLogin')->where('lang', '[a-z]{2}').
This way users can just change the language string and the site will load in the correct language (if supported).
Routes for different languages all lead to the same controller action. For these languages except english, I need translations (routes.php in /app/lang).
2) a before filter for those controllers whose actions get translated is set in constructor. It basically checks if the language string is valid and replaces it if not. The chosen language will be set in the session.
I hope anybody can use it :)