Links in Laravel - 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.

Related

Passing more than one parameters in route messes up my view

I am a beginner to laravel. When I pass more than one parameter through the route my view gets messed up. For example when my route is {{URL/product}}. It is ok, but when try it the other way i.e {{URL/product/anything}}, the view is messed up.
[This is the correct view][1]
These are my routes :
(Working)
Route::get('/addproduct' , 'AdminController#add_product')->name('add_product');1
(not working)
Route::get('/add/product' , 'AdminController#add_product')->name('add_product');This one is not working
I haven't changed anything but my web.php file.
This is probably because of how you are linking to your assets. You are most likely using relative URLs. Try using one of the URL helpers to generate absolute URLs for you.
href="{{ asset('css/somefile.css') }}"
Laravel 7.x Docs - Helpers - URLs asset

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

URL Routing: 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.

Found a issue related to Laravel routing

I have issue related to Laravel routing. For example if my site is xyz.com and i have link on my home page Instagram.
Then it does not take me to the Instagram link instead of opening this link it opens the wwww.servername/laravel/public/https://www.instagram.com/myprofile/ instead. How can I open a new link?
Your link is being generated using blade functionality. However based on the discussion in the comments above, the link does not sit within a blade file, and thus the blade functionality, specifically in this case url, is not being called.
Please ensure the following:
Blade functions sit within a .blade.php file
url is called within the blade markers {{ and }}
Homepage - This will take u to homepage
This post - This will take u to your link in this case this post
Best Regards

Can Laravel Blade be used in a sandbox environment?

Can Laravel Blade be used in a sandbox environment, similar to Twig's sandbox extension?
I have the need to allow users to use a template system but obviously do not want them executing arbitrary PHP code on the server.
I would like to use Blade since it's already part of Laravel but suspect this isn't possible.
The short answer is no, by default.
You could probably implement this yourself however, by extend blade to add custom functionality. See the Blade Documentation.

Resources