Laravel resource routing in subfolder - php

There are a lot of questions like this, but I don't find exactly what I ask.
So, I want to use resource routing, but in subfolder.
Route::group(['middleware' => 'adm', 'prefix' => 'adm'], function () {
Route::resource('users', 'Adm\UserController');
});
In this case, all actions without params works fine (index and create), but if I go to /adm/users/show/1 I got error NotFoundHttpException
Looks like it needs route vars in response manually, because if I don't use subfolder (adm) it works fine. What I'm doing wrong?
I want to use resource routing because there will be many controllers like users, projects, prices, rooms etc with standard CRUD actions.
Laravel 5.4

(GET) /adm/users/show/1 is not a route provided by your users resource controller. show is the Controller method, but does not appear in the URL:
https://laravel.com/docs/5.4/controllers#resource-controllers
You're after (GET) /adm/users/1.
There is no problem using resource routes in subdirectories, as long as you watch out for conflicting routes.

Related

how to generate short link in laravel framework and relation with 3 model

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.

Laravel taking url string as parameter

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.

Laravel Routes ordering

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

Laravel - Routing issue with resource

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.

Laravel sub action 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.

Categories