Laravel taking url string as parameter - php

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.

Related

How can I post data from a raw Php page to a Laravel controller?

I have a laravel project with a raw (. php) page. On this page is a form. I would like to post this form from the raw php page to a controller I have. How, if possible can I achieve this?
So after looking around online, I found the answer to be that I needed to use API routes. The file is located in Routes/api.php. One can add their routes there so requests from outside the Laravel application can be sent in. Simply add your route there and corresponding action for said route.

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 - 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