URL Routing: Laravel - laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.

Other than the actual look of the URL, there's no real difference as far as the framework is concerned.

I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically

No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).

There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

Related

codeigniter 4 enable users to define their own routes

I've started with Codeigniter 4 today. I have an app that I've built with Codeigniter 3 and now I want to upgrade it to CI4. The first thing is to upgrade routes. I have a simple but GREAT solution by enabling users to define their own routes with a simple page, like this:
And it was super easy, just needed to load and update Routes.php file - arrays of routes.
Now, as I am looking at app/config/Routes.php in CI4 I see it is not a simple array but there is much more to it. So, my question is what would be the simplest way to enable users to define their own routes with a webpage?

Links in Laravel

I'm am just starting out in Laravel, been using codeIgniter for years. Very quick question as the Laravel documentation does not seem to address this. Do all links in your blade file have to have defined route in the routes files. In codeigniter it was generally /controller/function in your links but it seems to me in Laravel all links have to be defined in routes file...
No, they do not have to be defined.
There's nothing prohibiting you from using <a href='/whatever/you/want'> -- That said, it's generally better to use defined routes and reference them by name, that way, if you ever change the actual structures, the route('name'); will automatically resolve to the new structure.
You can use
{{ url('/what/you/want') }}
https://stackoverflow.com/a/42270157/4173464
Look at https://laravel.com/docs/7.x/urls too
You must define all your routes in Laravel.

How to avoid overwriting routes in Laravel?

Sorry in advance, I know it has been asked before but I did not figure out the solution.
I am new to Laravel, still learning and stuck with this issue:
My objective is to add pages in admin and show these pages in frontend.
For the Front part of the website I have this route:
Route::get('/{page}', 'PagesController#show');
so the when you access /about, /contact, /another-page I use the same view
For the Admin part of the website I have this route:
Route::get('/admin', 'AdminController#show');
My problem is that the first route overwrites the second route and I don't know how to avoid this.
I have tried with namespaces and grouping routes, but I get the same outcome.
Thank you
To make it simple this is happening because you have the route with the parameter before the admin route so is going to send the "admin as a parameter of page"
The Simple fix is just put admin route before your "/{page} so it will find admin route first,Something Like this:
Route::get('/admin', 'AdminController#show');
Route::get('/{page}', 'PagesController#show');
But I do not recommend building your routes this way and have specifics pages setup if possible, This way of building routes will mess around with the 404 route not found aswell.

Laravel routing pattern similar to Yii

I am new to Laravel and by reading a bit its documentation, it seems that for each new page or url I need to define a route in the web.php file. Please correct me if I am wrong.
So, my question is... Is there a way to create a pattern as in YII framework or .NET framework to manage all routes (or most of them) at once by using something like:
{controller}/{action}/{id}
Well not exactly like Yii or .net but you can declare multiple routes in one line using Resource Controllers.
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application.
You can do so by Route::resource('photos', 'PhotoController');
Read more here

Laravel limiting access to route

I am trying to implement a basic image fetch system for my website. Already created a route that returns me the image.
what concerns me is that i want that route to be only accessible by certain controllers.
Tried to search it and found out passport might be viable option but it's pretty complex for this app. Are there any possible options ?
EDIT:
Sorry for providing insufficient information. I want the route to be accessible only by CONTROLLERS, not by anyone who enters the route url to address bar. Like using it as an api maybe.
There several ways to achieve that, you can use middleware, you can consider using packages like entrust which also require you to have some knowledge about using middleware. or use laravel Auth
create a table add all the routes in that table and then check the allowed route in AppService provider.
$routename = Request::route()->getName();
$allowed_route = AllowedRoutes::where("route","=",$routename)->count();
if($allowed_route == 0)
exit();

Resources