set site root in laravel 4 so / works properly - laravel-4

I have my laravel app inside www/mylaravelapp folder, and if I use / for home link, as in HOME, for some reason it goes to localhost, not localhost/mylaravelapp.
Obviously it is not smart enough to do this on its own, so how do I tell it to?

A good practise of Laravel is always to name the routes you define on routes.php
For example:
<?php
Route::get('/', array('as' => 'index', 'uses' => 'thisFunction#ThisController'));
Route::get('profile/{user}/edit', array('as' => 'user.edit', 'uses' => 'showEdit#UserController'));
And that way on your views, you can use:
HOME
Edit profile
And if one day you decide to change the URLs in the Routes, as long you don't rename them, you don't have to care about the links in the views, they will always be correct.

Best practice in laravel is to generate paths thru path gererator. I had a problem with routing when added parameter to url. For example /main worked fine, but in case of /main/1 page styles crushed. Thats becouse src of includes have been changed from domain/folder on domain/public/folder. I know it do not hit subject directly, but folder structure and links do with helper methods to avoid that kind of problems

Best answer I found was on Laravel forum, basically if / is not working properly then use url() function which seems to work fine.

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

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 5.3 session cookies are not creating in browser

I am facing token mismatch issue in new server while working fine in localhost. I have tried every possible way to clear cache and give SESSION_DOMAIN path in env but all seems useless.
Also session cookies are not being created in web browser while creating in storage/framework/session folder.
Please help me !
Are you getting tokenMismatchException exception?
If yes, some of the possible reasons are:
Check your files for PHP end tag "?>", if exists remove it. For more detail refer to this link.
You may need to use web middleware. For more detail refer to this link (although it is about laravel 5.2 but, it may work for your situation too).
Another thing to try is checking for web middleware presence. Normally it should be automatically applied to routes/web.php
Route::group(['middleware' => ['web']], function () {
<routes>
});
Also check out https://laravel.com/docs/5.3/upgrade to see if you have any code that might have been influenced by this update.
And lastly, it would be nice if you could post a piece of code which is responsible for sessions in your app.

Laravel version 5.3 removed routes.php

I am new to Laravel and trying to follow some tutorials. Apparently the old routes.php has been removed and another completely different process is in place.
What do I do when a tutorial wants me to make some changes to /app/Http/routes.php? I even created a routes.php file in the path mentioned but it didn't seem to work.
There is now a routes folder. Within is the web.php file. This is the new routes file. You can make all your routes within web.php the same way you did previously in routes.php. More info here: https://laravel.com/docs/5.3/routing

Resources