How to Mask a Route with another Route in Laravel - php

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>

Related

how to use route to view static apidoc page with laravel

I am currently using http://apidocjs.com/ as my laravel apidoc because I am just used to it before.
to view the apidoc, each time I have to drag the index.html into the browser which is quite annoying.
Is it possible to make it into a static page so people I am sharing the apidoc with can just generate the doc then go to the route.
I tried something like...putting my apidoc folder under public folder of the application and also tried adding a route such as
Route::get('/apidoc', function(){
return File::get(public_path() . '/apidoc/index.html');
});
Both of them didn't work the index.html cannot load the css and js because in index.html the source url is something like vendor/polyfill.js which then tried to go localhost:8000/vendor/polyfill.js but actually the url should be something like localhost:8000/apidoc/vendor/polyfill.js
Does anyone know how to easily fix this?
Thanks in advance for any help
You can "cheat" a little bit by registering the vendor routes as well:
Route::get('vendor/{any}', function ($any) {
abort_unless(is_readable(public_path("apidoc/vendor/$any")), 404);
return File::get(public_path("apidoc/vendor/$any"));
})->where('any', ".*");
Route::get('apidoc', function(){
return File::get(public_path() . '/apidoc/index.html');
});
Of course the ideal solution is if you actually manage to change the template you use for index.html to use relative and not absolute paths (i.e. change all <link href='/vendor...'> to <link href='vendor...'> so the file can automatically request the correct resource.

Returning javascript view in Laravel

In Ruby on Rails In my controller I can do something like this:
def jstest
respond_to do |format|
format.js
end
end
This would return jstest.js.erb from the relevant views directory
jstest.js.erb
alert("Hello World")
The result being I get hello world alert.
I want to do the same thing in Laravel
In my controller I have:
public function jstest( )
{
return view('jstest');
}
in my view (which I have tried with both jstest.blade.js and just jstest.js)
but I get the error, view cannot be found.
The only way I get this to work is by calling the view jstest.blade.php and including my js in a <script> tag within this php file. but this feels a bit wrong...?
Is this even possible in Laravel? If so, where am I going wrong?
Example use case:
Imagine the following example, I have a table of comments, a user can click a delete button which will send an ajax request to delete the comment.
My Route:
Route::delete('post/comments/{comment}','commentsController#delete');
In my controller:
Public Function delete($comment)
{
$comment->delete();
return commentDeleted.js
}
commentDeleted.js:
$(#comment).remove();
So from the users perspective, they click delete and the comment disappears from their screen without loading a new page.
I don't know if Laravel supports this out of the box, but this is my take on your issue.
This is how I understood your problem: you want to specify a path to .js file as an argument to a function and you want this .js file to be immediately executed when loaded via browser.
I'd do it in these 2 steps
Create a jsview() function that accepts a path, similar to view()
Create the base .blade.php file which will be used by the above
The .blade.php contents
Assumption is that this view will be named javascript.blade.php and it's in resources/views/ directory.
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="{{$path}}"></script>
</head>
<body></body>
</html>
The function
function jsview($path)
{
return view('javascript', ['path' => $path]);
}
Usage
<?php
Route::get('/test', function()
{
return jsview('/assets/my.js');
});
This answer isn't really fully complete because your browser will be loading files from public/ directory so all .js files you specify this way would be read from there. I believe Laravel does have something related to resources, but I'm not expert enough on that matter.
This is posted only to outline how to achieve something that you need, but isn't available out of the box.

How can we link view to view without the help of controller

I am new to CodeIgniter, and I don't know whether this is possible or not. How can I link view to view without the help of controller just like PHP.
{ <a href=''>contact.php</a> }
I tried base_url(), site_url() and current_url() but this error displays:
You don't have permission to access /Buildon/User/views/contact.php on this server.
When your using codeIgniter site url or base url etc. You should send them to the controller.
CodeIgniter Doc's http://www.codeigniter.com/docs
URI Routing: http://www.codeigniter.com/user_guide/general/routing.html
URL Helper: http://www.codeigniter.com/user_guide/helpers/url_helper.html
Lets say
Contact Us
The contact_us in the base_url would be controller name.
Or Example
Example
Example
If you try to send the link to view file will not work.
Incorrect
Example
Copy this file in root of this folder parallel to index.php and then you can access it.

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.

how to give an anchor point in Laravel view

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';
}

Categories