button redirecting to wrong path - laravel

I am using a link button in my blade view. The code is as follows:
<a href="conclusion" class='btn btn-default btn-sm'>End Case</a>
The url of the page on which this link button is:
http://localhost/cases/137/responses/30
The result of my button should be
http://localhost/cases/137/responses/30/conclusion
but it is redirecting to
http://localhost/cases/137/responses/conclusion
My laravel route definition is:
get('/cases/{id}/responses/{respId}/conclusion', 'HomeController#conclusion');
What is wrong with it? How can I do it?

The save way to generate URLs is to always use Laravels helper functions. This way you don't have any problem with relative URLs as it always generates a full URL. In your case action() would be appropriate:
<a href="{{ action('HomeController#conclusion', [$id, $respId]) }}" class='btn btn-default btn-sm'>End Case</a>
Alternatively you can give your route a name:
get('/cases/{id}/responses/{respId}/conclusion', [
'as' => 'conclusion',
'uses' => 'HomeController#conclusion'
]);
And then:
<a href="{{ route('conclusion', [$id, $respId]) }}" class='btn btn-default btn-sm'>End Case</a>

Redirecting is not relative to the current path. Its relative to the index.xxx. I suggest that you use a fully qualified path.

The issue is the current url is pointing to a document, not a folder: https://cdivilly.wordpress.com/2014/03/11/why-trailing-slashes-on-uris-are-important/
When using relative href's you actually point to a document in the current folder. While your current url has no ending slash (/) it's interpreted as a document and the relative url will actually replace the document name.
So at this time you are pointing to the document conclusion in the folder http://localhost/cases/137/responses/, because 30 is also "just" a document in that folder.
In order to fix your problem you should hit your page as a folder http://localhost/cases/137/responses/30/ which allows for relative url's href="conclusion" to point to http://localhost/cases/137/responses/30/conclusion
To ensure this works in laravel, your route that points to the response should have an ending /. An even better solution would be to generate the route to the pages using the laravel helper functions like route() and action(); see http://laravel.com/docs/5.0/helpers#urls

Related

How to correctly redirect with inertia in laravel?

I'm trying to redirect to another page with the click of a button, but for some reason it's not working, I'm not sure why?
Currently it opens up a window inside the main page not even a real redirect but it's blank. If I change the link to something else like just "/" or another page like "/users" then it works, but just for this link it's not working.
This is how I'm redirecting in the vue
<inertia-link href="imports/teachers">
<el-button type="warning">Import</el-button>
</inertia-link>
In my web.php
Route::resource('imports/teachers', 'ImportTeacherController');
In the controller
public function index()
{
return Inertia::render('Import/Teacher');
}
In the teacher.vue I have a very basic layout
<template>
<layout>
<div>
<h1>Page Title</h1>
</div>
</layout>
</template>
<inertia-link href="imports/teachers"> -> <inertia-link href="/imports/teachers">
You are missing a / before imports. It's just the same with normal anchor tags, you should add a leading /.
You must make <inertia-link href="/imports/teachers">
There is also an even better solution which I always use. With this you can use routing names just have a look at it then it goes like this <inertia-link href="route('import.teachers')"> https://github.com/tighten/ziggy
Or you add it manually how it works you can find here: https://inertiajs.com/routing at Generating URLs.

Laravel Routing to Views in Subdirectories

I'm using Laravel, and have a view structure like such:
Each of my pages have a navbar at the top, and the navbar has a few links on it:
<ul class="uk-nav uk-navbar-dropdown-nav">
<li>KENNEL INFORMATION</li>
<li>SIRES & DAMS</li>
<li>LITTERS</li>
<li>HELP</li>
</ul>
If I click one of these links (lets say info) from the homepage (home.blade.php), it redirects to the following url:
localhost:8080/mykennel/info
However, if I'm already on a view that is in the mykennel subfolder (again, lets say I'm on the info page), and I click a link from the navbar, it redirects to:
localhost:8080/mykennel/mykennel/info
Which throws a 404. I understand WHY this is happening but I can't seem to find how to fix it. How can I create an href in my anchor tag that knows to use only a single /mykennel/ prefix, regardless of where the user is currently situated on the site?
Any help is appreciated.
Try to use an absolute path instead, by adding / at the beginning of the href so links:
<ul class="uk-nav uk-navbar-dropdown-nav">
<li>KENNEL INFORMATION</li>
<li>SIRES & DAMS</li>
<li>LITTERS</li>
<li>HELP</li>
</ul>

Relative redirecting with or without the trailing slash

Basically the problem I'm facing with my laravel server is very basic, but I somehow still struggle with that. It's all about relative redirecting. Let's say for example, I have following uri: http://testsite.dev/configure. It's returning a view and I have some link there. I'd like to make the link send me to http://testsite.dev/configure/element-2
But if no matter what href I put to the a tag, it always sends me to http://testsite.dev/element-2. I've tried:
<a href="/element-2">
<a href="element-2">
<a href="../element-2">
<a href="/../element-2">
Each time without success. So I finally came up with force putting a trailing slash at the end of the /configure/, but I doubt that's a good solution. Could you suggest the best way to do my relative redirecting? I basically want links to add up to the current url and not to rewrite it. Answer should be somewhere in the htaccess file... Thanks in advance!
You can use the following:
Configure
Configure
all {{ function }} does is echo the function.

codeigniter path issue

I am trying to link my view page to another controller.
my test_view.php page
//this page address is base_url/controller1/function1
<a href='controller1/function2'> test </a>
If i click, the page address will be base_url/controller1/function1/controller1/function2 which is not my desire.
my controller
//the first function1 is to show my test_view page
function function1 (){
$this->load->view('test_view');
}
//I can't get to this function2 with the link I used
function function2 (){
$this->load->view('funny');
}
Anyone could help me about this? Thanks a lot.
Sure--you just need to tell CodeIgniter to display the path:
<a href="<?php echo site_url("controller1/function2");?>">
One thing: This displays the absolute path of your site as defined in your config, not the relative path.
I prefer relative paths, so I like to create a universal function called site_path to do the same thing without the absolute URL. I include it in one of my universally loaded libraries and it looks something like this:
function site_path($url) {
return "/$url";
}
The benefit of this is that, if I initially develop the site in a subdirectory, I can set site_path to return "/subdirectory/$url" and then just remove the subdirectory once I launch.
It's linking to a relative URL, you need to start with a '/' to use the web root
<a href='/controller1/function2'> test </a>
you can use following code in test_view.php page,
<a href='<?php echo base_url();?>controller1/function2'> test </a>

CodeIgniter view anchor tag prepending host

In my view I'm trying to create an anchor tag but CodeIgniter is prepending my entire host before what I specify as href, therefor making it invalid.
I put this
My File
I get
My File
EDIT 3
This is my view, I realise i should not be calling a function in my view but in this case I had little option as something needs to get applied for each data item in the loop, I will try to change this but thats beyond my problem right now.
Wesley: I checked the soruce and it displays correctly in the source but in the browser it preappends the host so I guess this has nothing to do with code igniter afterall! How do I make sure it doesn't happen?
<td><?php
$this->ci = &get_instance();
echo $currentData["field_one"] . " - Log"; ?>
</td>
EDIT 4
my html source
<a href="file:://///\\myhost.local.com\120">
View Log
</a>
my url address bar
http://myhost.local.com/myhostlocal/index.php/level/one/type/b/cc/ee/
the url it goes too when I mouse over the href
http:///myhostlocal/index.php/level/one/type/b/cc/ee/file:://///\\myhost.local.com\120
Looks like the browser is treating "file:://///" as a relative path. Why do you have 2 colons, do you need both? Removing one will provide a valid protocol, and the browser will start treating it as an absolute path.

Resources