how to give an anchor point in Laravel view - php

I am using blade templating and I want to make the users navigate to a point on a page when they click on a link. This is my view :-
View::make($data->path)->with('data', $data)->with('title', $title);
My path is being fetched from the database.
There is a link:-
<a id="middle">Welcome to the user section</a>
Now I don't think putting #middle in make() will help :-
I tried this:-
View::make($data->path."php#middle")->with('data', $data)->with('title', $title);
and
View::make($data->path.".blade.php#middle")->with('data', $data)->with('title', $title);
but neither seems to work.
How can i make this work?

Your view is just the file to render -- you shouldn't confuse it with the request URL, which is determined by routes.
All you need to do is add the anchor to the URL the user clicks to get to this location, on the end of your route. If it's not something you can determine at the time the link is generated, you should use a redirect in your logic to the URL you wish to include.
That said, you could also pass a variable to your page that javascript uses to scroll the page to the appropriate location as well:
View::make($data->path)->with('data', $data)->with('title', $title)->with('scroll', 'middle');
Then, on your page, in javascript:
document.getElementById('{{ $middle }}').scrollIntoView();

override getRedirectUrl :
protected function getRedirectUrl()
{
$url = $this->redirector->getUrlGenerator();
return $url->previous() . '#hashid';
}

Related

How can I keep anchor alive in pagination links?

Here is my code in the controller:
$unique_products = unique_products::orderBy('id', 'DESC')->paginate(10);
And here is my code in the view:
{{$unique_products->render()}}
And here is my current URL:
http://www.example.com/products#anchor
My problem is, when I click on other pages (which are in pagination box), the anchor is deleted from the URL. It will be something like this:
http://www.example.com/products?page=1
Anyway, how can I keep the anchor exist? I want this:
http://www.example.com/products?page=1#anchor
How can I do that?
First of all you need to display your links like shown in the official docs because when you want to navigate through pages your unique_products has access to the fragment method which can retrieve fragments from the url which are actually Hashtags (#) and then append them to every page change url:
{{ $unique_products->fragment('foo')->links() }}

How to Mask a Route with another Route in Laravel

I want to redirect www.myexample.com/back when I call www.myexample/blog/tree without changing the url.
If I put the www.myexample.com/back in browser , it should load www.myexample/blog/tree with keeping the www.myexample.com/back in url bar.
In Laravel I tried
Route::get('/back',function(){
return redirect('/blog/tree');
});
Please assume that the www.myexample/blog/tree is an actual wordpress URL
But it changes the url
Is there any way to achieve this ?
I think you should use the Controller function of www.example.com/blog/free for route www.example.com/back. Two routes use the same function will return the same page. The redirect function will change the url, of course. For example:
Route::get('/blog/free','BlogController#free');
Route::get('/back','BlogController#free');
You can create a view /back and use an iframe to load the blog inside it:
Something like:
Route::get('/back',function(){
return view('back');
});
And then in the view back.blade.php looks like
<html>
<body>
<iframe src="http://www.myexample/blog/tree"></iframe>
</body>
</html>

how to put URL inside of laravel blade

I have database structure like Book & download link ..
$book= new Book();
$book->name = $request->Input(['name']);
$book->download_link = $request->Input(['download_link']); // eg. https://bnbooks.com/1234
$book->save();
now if i want to give this in blade page ..like download link..how do i put in href tag so that it will redirect the user to that page?
{{$book->name}}
// This doesn't work
URL::to is a function to create a url on your site from only the path (for example /foo/bar). Since you store the full URL, you can just do
{{$book->name}}
You can do something like
{{$book->name}}
to show the url.
If you have a url like /example/example and you also want to show the domain name in front of the url, you do
{{$book->name}}
This will create an url like localhost:8000/example/example (In case of localhost)
Hope it works
{{$book->name}}

Base_url() issue with the anchor tag in CodeIgniter

I am new to CodeIgniter and having trouble with link formation. In my page, I have a link to another page set to:
<li>Feed Page</li>
Here the feed is one of my controllers, but the link is showing as:
http://localhost/BusinessPad/localhost/BusinessPad/feed,
which doesn't actually exist. I can't understand how this happened; I have made sure: $config['index_page'] = ''; and I added an .htaccess file. If I leave $config['base_url']='', the base URL is still not working for me.
To deal with anchor tag, You can have like this
Feeds
You shall put the base_url inside your anchor tag and then your controller name.
Then you will get the url like localhost/BusinessPad/feed which you expect.
Note : Make sure you have loaded the url helper by $this->load->helper('url'); or loaded in autoload itself
Feed Page
if this not works use this
Feed Page
Explain :
<?php echo base_url();?>//URL
BusinessPad//Controller
feed//Function
in 1. this will give your site URL. http://localhost/BusinessPad/localhost/. if you host this to live server it will come with tour domain name.
then in 2 it will call your controller which Contains your functions.
Note: if you juct call your controller it will call automatically public function index() if you create.
then in 3 it will redirect to your function which you exactly need.

URL with trailing slashes leads to different pages - MVC CI

I have a routed like the following in my Code Igniter route file.
$route['profile'] = "profile";
The class profile contains the following
$this->load->view('pages/profile.php');
Now I have a link on my profile.php page which looks like the following
<a href = 'logout'>Logout</a>
Now consider a user using it. If he visits the following url
localhost/project/profile
Then now the link on the profile.php page would lead to the following
localhost/project/logout
But if the user uses this url
localhost/project/profile/
That is if there is a trailing slash then the link on the page would lead to
localhost/project/profile/logout
And now my question is what should I do to lead the link in both the cases to
localhost/project/logout
Hope I am clear. Please help me out
I think that you're searching for this, load the url helper on your controller with this code line
$this->load->helper('url');
On your view whenever you want to echo URL's use this code
//this will echo something like this http://(yourdomain).index.php/logout
<a href = '<?=site_url("logout")?>'>Logout</a>
If you want let's say, an url to another controller you'll use something like this
//this will echo something like this http://(yourdomain).index.php/(anothercontroller)/logout
<a href = '<?=site_url("anothercontroller/logout")?>'>Logout</a>
More information about the url helper from codeigniter can be found here:
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Categories