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
Related
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.
I have been learning Laravel recently and I have seemingly missed a key point: why should relative links be avoided?
For example, I have been suggested to use URL::to() which outputs the full path of the page passed as a parameter - but why do this when you could just insert a relative link anyway? E.g., putting URL::to('my/page') into a <href> will just insert http://www.mywebsite.com/my/page into the <href>; but on my website href='my/page' works exactly the same. On my website I have based all relative URL's from the index.php file found in the public directory.
Clearly, I'm missing a key point as to why full paths are used.
I've found that using route() on named routes to be a much better practice. If at one point you decide, for example, that your admin panel shouldn't point to example.com/admin, but example.com/dashboard, you will have to sift through your entire code to find all references to Url::to("/admin"). With named routes, you just have to change the reference in routes.php
Example:
Route::get('/dashboard', ['as' => 'admin', 'uses' => 'AdminController#index']);
Now every time you need to provide a link to your admin page, just do this:
Admin
Much better approach, in my opinion.
This is even available in your backend, say in AdminController.php
// do stuff
return redirect()->route('admin');
http://laravel.com/docs/5.1/routing#named-routes
Neither absolute nor relative links should be used - it's advisable to use named routes like so:
Route::get('my/page', ['as' => 'myPage', function () {
// return something
}]);
or
Route::get('my/page', 'FooController#showPage')->name('myPage');
Then, generate links to pages using URL::route() method (aliased as route() in L5), which is available in Blade as well as in your backend code.
That way, you can change the path to your routes at any time without having to worry about breaking links anywhere in your application.
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.
I am trying to create short links to my application in codeigniter but I've met a kind of a problem when designing my route. The problem is that I want a route which will take a string containing a-Z and numbers and redirect that to a controller called image with the string after. Like this: app.com/randomstring -> app.com/image/randomstring. But when I am trying to do this in the routes config file with a regular expression it disables my application and I am unable to enter "normal" urls with controllers that already exist.
How my route looks like right now (I know it's probably very wrongly made):
$route['(^[A-Za-z0-9]+$)'] = "image/$1";
Is there any easy way to redirect with that short url without using another fake controller first like this: app.com/i/randomstring -> app.com/image/randomstring
And could you maybe help me improve and tell me what part of my regexp is failing?
As I mentioned in the comments, without a clearly defined spec on what the image urls will be, there's no comprehensive way to solve this. Even YouTube (related to the library you linked to) uses urls like /watch?v=h8skj3, where "watch" is the trigger.
Using a i/r4nd0m$tring would make this a non-issue, and it's what I suggest, but I had another idea:
$route['(:any)'] = "image/$1";
// Re-Route all valid controllers
foreach (array('users', 'login', 'blog', 'signup') as $controller)
{
$route[$controller] = $controller;
$route[$controller.'/(:any)'] = $controller.'/$1';
}
unset($controller);
You might need the image route last, I'm not 100% sure. This should route everything to image/ except the controllers you define. You could even use glob() or something to scan your controller directory for PHP files to populate the array.
Another way to get one character shorter than i/string could be to use a character trigger, like example.com/*randomstring, but that's a little silly, i/ is much cleaner and obviously, easier to deploy.
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".