Help with Routes in Laravel 5.6

1

I've been working on a web application through Laravel in Windows (using Laragon ). I am new to using this framework and I have found an inconvenience:

When I use Routes as for example:

Route::get('/', 'PagesController@home');

It's all good though, if I do the following:

Route::get('casos/{nombre?}', 'PagesController@caso');

When updating the web in the browser, I notice that all my URIs of my scripts , css and even images that I use in the web view, change, before a folder that receives by name: casos/ .

In the same way it happens when doing it this way:

Route::group(['prefix'=>'admin','middlewere'=> 'auth'],function(){
     Route::get('donantes','Administrador\CasosController@home');
});

If I create a route in this way, on the web, to all my links css , js , images, etc. the prefix admin/ is prefixed to it.

How can I solve it?

    
asked by Jhonatan Malara 23.08.2018 в 12:59
source

2 answers

2

What happens is that html recognizes the routes of your files in a relative way, so it will look for those files right in the url indicated by your route.

Assuming you're trying to call a script.js file:

<script src="js/script.js"></script>

So if for example you are in admin/index it will be assumed that your script is located like this:

/admin
    /index.html
    /js
        /script.js

But if now your url changes to admin / donors / index, it will be assumed that your script is in another directory:

/admin
    /donantes
        /index.html
        /js
            /script.js

This is called relative routes , but what you need is to have < em> absolute paths to access the resources of your files.

However, Laravel and other frameworks have implemented functions helpers to give solution to this, in this case for urls you mainly have these:

asset

  

That generates a URL for a resource using the current scheme of the request (HTTP or HTTPS):

<script src="{{ asset('js/script.js') }}"></script>

route

  

That generates a URL for the name of a route:

<a href="{{ route('nombre_de_ruta') }}">Link</a>

url

  

That generates a fully qualified URL for a given route:

<a href="{{ url('/admin/donantes') }}">Link</a>
    
answered by 23.08.2018 / 15:40
source
2

I think the problem may be that you are probably loading the files with relative paths. as for example:

<script src="js/archivo.js"></script>

In Laravel there is a function in php that you can use to indicate routes and in this way the route is kept formatted correctly. This function automatically adds the domain to the beginning. You can read more about the multiple functions that exist here

But an example can be the following:

<script src="{{ url('js/archivo.js') }}"></script>

or

<script src="{{ asset('js/archivo.js') }}"></script>

I hope this works for you.

    
answered by 23.08.2018 в 13:33