How can I get a URL for a controller action? - php

I have a link that looks like this:
<a class="btn btn-primary edit"
href="Personas/details/<?php echo $persona['Persona']['id']; ?>"
data-original-title="Editar">
<i class="icon-pencil icon-white"></i>
</a>
As you can see I'm manually writing in the controller and action for the link. Is there some way to make this not as brittle?
Something like:
<a href="Url.Action("Personas", "Details", array('id', 1);" >Asdf</a>

Use: Html->url, does exactly what you want.

Related

Open modal when redirected only

Before, I had a view called "book-edit/[ID]" to edit a book but now I added the form to edit the book in the book details page. I have a button to edit in the list of books page and I want it to go to the book page and open de edit modal on click. This is what I have rn:
View: Books (datatable list with this buttons for each):
<a href="<?php setURL(); ?>./book/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="View Book">
<i class="fas fa-eye"></i>
</a>
<a href="<?php setURL(); ?>./copy-add/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Add Copy">
<i class="fas fa-plus"></i>
</a>
<a href="<?php setURL(); ?>./book-edit/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit Book">
<i class="fas fa-edit"></i>
</a>
View: Book/[ID] (I have modals with id "#editBookModal" and "#addCopyModal")
When you click on the books list to edit the book or add the copy, I want it to go to book/[ID] and automatically open the intended modal. But only when buttons clicked, if you go to Book/[ID] to view the book, it shouldn't open the modals automatically. That's why I can't just open it with jquery in the book/[ID] page...
Any idea of how I can do this?
Thanks!
One approach would be add hashes to the url in the links like:
....
Then on the other page check the url hash and if it matches a modal then open that modal
const modalHashes = {
'#edit-book':'#editBookModal',
'#copy-book' : '#addCopyModal'
}
let hash = window.location.hash;
// inside document.ready and after modal script has loaded
if(hash && modalHashes[hash]){
const modalSelector = modalHashes[hash];
$(modalSelector).yourModalShowMethod()
}
You could even remove the hash when the modal gets closed so if user refreshes the page it won't pop up again
// in modal close event handler
window.location.hash =''

Prevent quotation in window.open in PHP

I'm having a problem with my script. The file wont open if the filename has single quote.
Is there a way to prevent single quote in window.open?
<a class="btn btn-primary btn-xs" onclick="window.open('content_files/<?php print($contRow['cont_file']); ?>','<?php print($contRow['cont_file']); ?>','height:auto;width:auto;')">
<i class="glyphicon glyphicon-edit"></i> View
</a>
here's the output
7TATTOOED ON MY MIND CHORDS (ver 2) by D'Sound # Ultimate-Guitar
Sample Output 1.
Sample Output 2.
You should encode your filename I think:
urlencode($contRow['cont_file'])
I tried this code:
<a class="btn btn-primary btn-xs" onclick="window.open('content_files/<?php print(addslashes($contRow['cont_file'])); ?>','<?php print(addslashes($contRow['cont_file'])); ?>','height:auto;width:auto;')">
<i class="glyphicon glyphicon-edit"></i> View
</a>
and It works like a charm. thanks anyway. Credits to the one who deleted he's post but it works.

How to use bootstrap "modal box" instead of "<a> tag and new page"?

I can show details data in a new page using details.php using anchor tag like bellow :
<a class="btn btn-info" href="details.php?view_id=<?php echo $row['studentID']; ?>" title="click for Details" onclick="details.php"><span class="glyphicon glyphicon-eye-open"></span> Show Details</a>
Now I am trying to learn it to show using bootstrap modal box like code bellow:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Details</button>
I have put the code of "details.php" in #myModel section and how I can show details data using dynamic ID - comes from view_id= for the code I am trying?
Please help me to understand my mistake. Thank you.

Unknown parameter format in laravel

I have this route
Route::name('recruitment.edit.profile')->get('/edit-profile/{id}', 'EmployeeController#showEditProfileForm');
having a parameter id and
this code in my blade.php
<a href="{{ route('recruitment.edit.profile', $users->id) }}" class="btn btn-success">
<i class="fa fa-edit m-right-xs"></i> Edit Profile
</a>
But when I clicked the given link I got this url http://mpci-vo.dev/recruitment/edit-profile?9 and a message Whoops, looks like something went wrong.
My question why is there a ? before 9
Change route('recruitment.edit.profile', $users->id)
to
route('recruitment.edit.profile', ['id' => $users->id])
https://laravel.com/docs/5.4/helpers#method-route

Why the result looks strange when I send different parameters to the same function? (laravel 5.3)

My view is like this :
...
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create.year', [$year]) !!}">
Add New
</a>
...
...
#foreach($testArray as $key)
...
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$key['display'], $year]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>
...
#endforeach
...
My routes is like this :
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
My controller is like this :
public function create($year, $display = null)
{
echo $display.' : display <br>';
echo $param_thang. ' : year';die();
...
}
When I call url like this : http://localhost/mysystem/public/users/create/2016, it works.
There result is like this :
: display
2016 : year
When I call url like this : http://localhost/mysystem/public/users/create/14144499452111901/2016
The result is like this :
2016 : display
14144499452111901 : year
It looks strange, the result is reversed
Why did it happen?
It's because your route is:
Route::get('users/create/{year}/{display?}', 'UserController#create')
->name('users.create.year');
So the first parameter is always going to be the year, and the 2nd parameter is always going to be the display variable.
You need to change your <a> as #farkie suggested:
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$year,$key['display']]) !!}">
<i class="glyphicon glyphicon-plus"></i>
</a>

Categories