Laravel routing and route parameters - php

I have route defined in my route.php file like below:
Route::get('{test1}/{test2}/{test3}', function($test1, $test2, $test3) {
$result = [$test1, $test2, $test3];
return view('view', compact('result'));
});
it works fine in my controller but on the view part when i see it in the browser when i just write something like this in browser:
http://localhost/mysitefolder/public/test1/test2/test3
it loads the view and pass all the data but it gets all of my assets like my stylesheets, images and scripts from a url like below:
http://localhost/mysitefolder/public/test1/test2/js/jquery.js
why is that happing?
thanks in advance!

Two solutions either use a / in the start of your URLs so that relative addressing is not used or use laravel's helper functions {{ asset('/js/script.js') }}.
When you do not use '/' in the beginning your url, it is treated as if it was a relative address and current location is added in its start.
Sometimes even '/' won't work if your application is not served on Root level. For example you have your application at http://localhost/yourapplication then / would refer to your localhost instead of application, that's why best way is to use laravel's helper function.

Related

Shorten URL codes using middlewares in Laravel

I still want my codes in public directory, and since I have to use the school's computer, I can't make the URL from localhost to something else.
Example: whenever I type URL for href, I have to put it like this. (larsamp is my project name, posts are the param)
About
But I just want to type this instead
About
Here is the routing PHP:
Route::get('/about', 'PagesController#about');
I found some solutions, but they do not work.
Change public directory to shorten the public part: I have another directory called private to store many things else.
Create a global variable (i.e. $u) and put it in the layout directory: but I have many layouts.
Overload href or make a function inherit href and use it instead: my teacher said that will make my program too complex. Not good.
In conclusion, a way to shorten URL without making the program too complex and can be used on a school computer (no changing host.txt)
You can use the url() helper which returns a fully qualified url:
<a href="{{ url('about') }}>About</a>
https://laravel.com/docs/5.8/helpers#method-url
Alternatively there is also the route() helper:
Route::get('/about', 'PagesController#about')->name('pages.about');
<a href="{{ route('pages.about') }}>About</a>
https://laravel.com/docs/5.8/helpers#method-route

Locate bower_components in laravel and using loadbalancer

I would like to know how can I call all my scripts file inside /public/bower_components in laravel? I'm having problem with the loading of all my scripts since we are also using a loadbalancer. Here's a sample of my code
URL: http://10.0.2.3/transaction/ <---- my project url where 10.0.2.3 is our loadbalancer/haproxy public IP and 'transaction' is the keyword use to reroute to our private server.
Now when I use this laravel script
{{ Html::script('bower_components/angular/angular.js') }}
I assumed that this will look for the angular.js file in the url
http://10.0.2.3/transaction/bower_components/angular/angular.js
but upon checking my file its looking in this url
http://10.0.2.3/bower_components/angular/angular.js
as you can see the keyword 'transaction' is removed. Is there a way to fix this so that it will look for the file in the correct path with 'transaction' included in the url?
You can just write a custom helper to prepend the route
if(! function_exists('asset_lb') {
function asset_lb($path) {
return asset('/transaction/'.$path); // <--- you need to customize the PATH Separator.
}
}
then use in project
{{ asset_lb('bower_components/angular/angular.js') }}

Laravel - Dynamic path

Apologies in advance if this is a noob question
I have a delete function in Laravel, I am having problems with the route and returning variables I need when the website is hosted externally.
I currently have the URL coded like this
Delete</td>
I would like this url to be dynamic. I have tried the public_path() function but have had no prevail.
Delete</td>
Thanks
You should use the routing system to generate urls ,and not create them by hand, it's quite easy:
http://laravel.com/docs/routing
the concept is simple:
1) create a route in the routes.php file
2) use it with the route() helper
edit:
to use a route and let LARAVEL generate the correct url , you simply have to do something similar to this (supposing that you are using blade):
Delete
You can use url() for example, like this:
Delete
You can also call link helpers:
{{ link_to('safesign_doc_delete/'.$file.'/'.$sector_name.'/'.$selected_sector, 'Delete') }}
url() is the default helper function get your hosting url which is found in the app.php under 'url' => 'http://localhost' (or other)
This will solve your URL problem
NOTE
if you are worried about changing it when it goes to production don't worry.
Laravel gives you folder options where you can have LOCAL as a folder with localhost as your url and then the main app.php file can be your production settings.

Define base url for laravel

I am new to laravel. My url for my only controller is
http://localhost/myapp/public/users
In the views for the form functions, I don't want to have to do an absolute path to the post function, e.g.
<form method='post' action='/myapp/public/user/update-user'>
I want to just set the users controller as the base url path so I can say in my form
<form method='post' action='/update-user'>
Is this possible?
Use URL::to()
or simply use laravel form
{{Form::open(array('url'=>'route_name'))}}
You can use URL::to() function.Laravel supports RESTful API or RESTful routing. So it is not a good idea to use routing like this.So use like,
In app/routes.php
// for view (GET)
Route::get('users', function(){
View::make('users');
});
Route::post('users', function(){
// do form validation and process here or call a function from controller
});
Then in your form use,
echo Form::open();
// form elements
echo Form::close();
Yes, it is possible, but it's a little complicated because laravel isn't really set up to support this.
You need to change the file vendor/laravel/framework/src/Illuminate/Http/Request.php, replacing the function root() with this:
public function root($domain = NULL)
{
return rtrim(($domain?:$this->getSchemeAndHttpHost()).$this->getBaseUrl(), '/');
}
And then in the getRootUrl($scheme, $root = null) method in the file vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php you need to change this line:
$root = $root ?: $this->request->root();
to this:
$root = $this->request->root($root);
That should allow laravel to automatically detect and handle subfolders correctly, so you will be able to use the standard URL methods.
First, you need to hide the /public/ part of your URL using the virtual host or the alias in your web server (apache or ...). The entry point (http://some.host/alias/ or http://some.virtual.host/) should lead directly to the public folder (i.e. /whatever/path/myapp/public/).
Second, using the routes, you can do the remaining easily. The route
Route::get('update_users', "UsersController#updateAction")
will redirect the http://whatever.host/update_users to the correct action without the need of the ugly format.

Make title string the entire URI for all pages using CodeIgniter

With CodeIgniter I'm trying to create a URL structure that uses a title string as the entire URI; so for example: www.example.com/this-is-a-title-string
I'm pretty confident I need to use the url_title() function in the URL Helper along with the routes.php config folder but I'm stuck bringing it all together.
Where do I define the URI and how is it caught by the routes folder?
Seems to be a straight forward problem but I'm getting stuck creating the URLs end-to-end. What am I missing?
I thought about a catch-all in the routes folder: $route['(.*)'] = "welcome/controller/$1"; ....but how would this work with multiple functions inside a particular controller? ...and maybe it's not even the right way to solve.
You can send all requests to a driver with something like this:
$route['(:any)'] = "welcome/function";
Then use the _remap function to route requests inside the controller.
However, using URL's as you suggest limits the CI functionality. Try something better like www.example.com/article/this-is-a-title-string
$route['article/(:any)'] = "articles/index";
and in article (controller), use _remap...
If you're going to re-route every request, you should extend CI_Router.
The actual implementation depends on what you're doing. If you customize CI_Router, you can do it AFTER the code that checks routes.php, so that you can keep routes.php available for future customization.
If the URI contains the controller, function, and parameters, you can parse it within your extended CI_Router and then continue with the request like normal.
If the URI is arbitrary, then you'll need something (file, db, etc) that maps the URI to the correct controller/function/parameters. Using blog posts as an example, you can search for the URI (aka post-slug in WordPress) in the db and grab the corresponding record. Then forward the request to something like "articles/view/ID".

Categories