Laravel - Routing issue with resource - php

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.

Related

Laravel resource routing in subfolder

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.

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

How can you check if a laravel route can be accessed before calling it

I have a laravel 4.2 app. And I have filters on many of the routes.
I am trying to display navigation and I would like to be able to check that a link can be accessed before displaying it.
I can't see a way to check that a routes before filters would pass given a URL.
Is this possible?
I have tried to get the route using the method in this question
But calling callRouteBefore() on it doesn't seem to give the expected results. I think it is due to the session not being right.

Laravel, how to use another controllers to the same folder

I have a big doubt building the structure of my application using Laravel because I want to use two controllers using the same folder, for example, this is my current structure:
Folder structure
app/views/dashboard.blade.php
app/views/settings.blade.php
app/views/business/dashboard.blade.php
app/views/business/settings.blade.php
Routes.php
Route::get('/user/dashboard','HomeController#dashboard');
Route::get('/user/settings','HomeController#settings');
Route::post('/user/login','UserStandardController#login');
Route::post('/user/logout','UserStandardController#logout');
Route::get('/business/dashboard','BusinessController#dashboard');
Route::get('/business/settings','BusinessController#settings');
Route::controller('/','HomeController');
Route::resource('/business','BusinessController');
Route::resource('/','UserStandardController');
Route::resource('/','UserBusinessController');
Basically:
HomeController is used for loading the views for the standard user.
BusinessController is used for loading the views for the business user.
UserStandardController and UserBusinessController are used for the actions for those accounts, for example: login, logout, update settings, update profile, etc.
But the problem is when I try to load mysite.com/user/login, laravel says "Method [login]" doesn't exists and is obviously because in HomeController doesn't exists the login method but exists in UserStandardController... but I don't know how to do this.
I hope you can help me!
You only have POST route for /user/login, you need to have Route::get also.
You have to create a login method in UserStandardController class for this error to go away.
As /user/login is searching for a login method in UserStandardController.
I hope this helps.

Categories