I am using Laravel 4.2 on a project.
I have setup routes for :
slug, category, services
In terms of ordering, If I move services above slug, It can't find the slug and ultimately goes to to the services controller and same, If I keep the slug method above services. It can't find that either.
My current routes is as follows :
Route::get('{slug?}', 'HomeController#index');
Route::get('{category?}', 'HomeController#category');
Route::get('{services?}', 'ServicesController#index');
Is there a way to fix this, The category one works fine, But switching slug and services around creates a problem.
Thanks
They way you wrote the routes, Laravel has no way to tell which request should go to which route, because you wrote three routes with one optional parameter which could be anything.
The way routing works is: it applies the first route that matches the pattern. That is why when it hits the first route pattern that says "one param that can be anything" it picks that route.
You should give Laravel some kind of hint, like:
Route::get('{slug?}', 'HomeController#index')->where('slug', '(blog|home|contact)');
Route::get('/category/{category?}', 'HomeController#category');
Route::get('/services/{services?}', 'ServicesController#index');
If you restrict the possible values or if you put additional part in the URL, Laravel will be able to recognize which route should go to which handler.
Take a look at the docs to get more ideas
Related
I have created a short link system for my site, now how can I link it to my three models my Models
Article
Post
Music
i use this toturial for short link => https://laracasts.com/discuss/channels/laravel/generate-short-link#:~:text=some%20idea%20from-,here,-Reply
That's easily done through routing.
So in your route file it will be something like this...
Route::get('music/{slug}', ['as' => 'music', 'uses' => 'music\MusicController#music']);
Then anything that comes in to your domain as https://yourdomain.com/music/slug will go to that music controller and you can process what slug is, then grab the model stuff you need and shoot them to a view.
If you really want nothing but short urls meaning https://yourdomain.com/shorturl you'll have to put the wild card at the root. This could have some undesired effects though if you don't have your routes set up in order. I'd probably put some middleware on the group too to process the shortUrl code and then send it on through to the controller where you can handle bringing back the model and shooting them to a view.
So I am working on a project and I have decided to use php laravel framework, but when it comes to things like creating a new page and dealing with page redirects etc, am I right in thinking that all these will be handled in the route/web.php file so all the pages for my application will be defined in the route along with the view ?
I was thinking what if my application grows to have dozens of pages is it best practice to define each one on the route or are there better ways to handle this?
Yes, the best way to do routing is laravel to have each route for . each page, the only exception is when you have dynamic routes, for example, if you have a route that checks for users id or category for some product etc and it looks something like this Route::get("/product/{$category_id}","FrontController#methodForGetingProduct").
And later in Controller, you define what will you send and receive of the information and what view should be returned.
you can follow this type routing .......
So basically i want to create a route like http://example.com/{page_slug} on my laravel app, but when i register that in my routes file it affects pages like http://example.com/login as it give me an error related to page "login" not found.
Here is my route with the page slug parameter
Route::get('/{slug}','Admin\PageController#viewPage');
The login route id generated automatically, i'm using laravel 5.4
Any help is appreciated
I found the solution:
All i had to do was put the
Route::get('/{slug}','Admin\PageController#viewPage');
As the last route, so other routes would be checked first.
Im new to Laravel and just ran into a problem. I got these two routes.
Route::get('posts/{id}', 'PostController#show'); // To show the post
Route::resource('users', 'UserController');
The problem is when i want to go to /posts/create tries to send me to the show function, but ofcourse can find the object. What am i doing wrong? So i thinks the word "create" is an id.
Hope you can help me out.
Right now you do not have RESTful path binded to correct route.
I recommend you to define posts also as resource like this
Route::resource('posts', 'PostController');
Now you have all the RESTful paths automatically created and calling /posts/create will be handled by the create method in controller.
I'm building an admin section and I want to keep urls looking nice but more importantly my code well structured.
I currently have a URL like this
/admin/
which works fine. Another URL like this
/admin/users
which also works fine
However what I now want is an admin page to add a new user which would have the url
/admin/users/add
I cannot seem to get anything to change with that URL, it always triggers the admin/users controllers.
Would appreciate any help
Have you set up a route for this? Controllers by their nature allow you
{Controller Name} / {Controller Action}
To set this up the way you want them you will need to use a route
Route::get('admin/users/add', 'admin#user_add');
This will route all requests to 'admin/users/add' to the admin controller using the method 'get_user_add' or 'action_user_add' depending on whether the controller is restful.