how to use route to view static apidoc page with laravel - php

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.

Related

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>

Silex and Twig: global.request.baseUrl is empty

My URL is looking like this:
www.MyWebsite.nl/Escape/public/user
So in my twig template, I should be able to use the following line for an image:
<img src='{{ global.request.baseUrl }}/img/EscapeRoom.png' class="slide">
But for some reason that does not work... the baseUrl should contain:
/Escape/public
But it is empty, can anyone tell me why and how to fix it?
EDIT
When I go to the following page:
www.MyWebsite.nl/Escape/public/user/login
Css, images etc. don't work anymore, since the path isn't correct anymore (two words after 'public'). Do you know how that problem can be solved?
To begin with, the way that you are structuring the URL is fundamentally wrong. You should use absolute URLs that URL Generator in Silex produces that is bound to the respective route.
Register URL Generator:
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
Name your routes:
$app->get('/', function () {
return 'welcome to the homepage';
})->bind('homepage');
In your template code, to get the URL of the homepage, use
{{ app.url_generator.generate('homepage') }}
Also, you need to use .htaccess based URL rewrite rules on your public directory to beautify URLs. That will help you to map your static assets simply by writing the path like below.
<img src='img/EscapeRoom.png' class="slide">
This requires that you use .htaccess file to rewrite your site URL and point the public directory as your site root in your hosting provider.
Silex - URL Generator

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.

Laravel: Navigation in 1 blade file

I am currently working on a new project, and for the first time I am using Laravel. I've created several blade files (views) and added the routes of the views. Because there are so many pages I would like to have my Navigation content in one file, called a base file. I extend the base.blade.php file in every view so my navigation is everywhere the same. But, I have now the problem that some links are not valid, and have no route (because they are pointing from different locations). What is the best way to link to other blade files (and views) in one single file? What I have now:
my route
Route::get('/jaarhoroscoop', function () {
return view('jaarhoroscoop');
});
my nav file
<li>
<a class="headnavitem" href="jaarhoroscoop">Jaarhoroscoop</a>
</li>
So when I am on the index file, it will link me to the view of "jaarhoroscope". But when I am on a page of a different view and in a other directory..
Route::get('/chinese-horoscopen/jaarhoroscoop-2016-2017', function () {
return view('chinese-horoscopen.jaarhoroscoop-2016-2017');
});
Like the view above, it will take me to public/chinese-horoscopen/jaarhoroscoop. I cant get it right.. Who can help me out? What I would like is that if I am on above route, I would go to public/jaarhoroscoop instead of public/chinese-horoscopen/jaarhoroscoop.
Thanks!
If i understood your problem. When you are in (root) / and click as an example 'categories' the roote becomes /categories.
When you are in /categories and click an other navigation link it appends it to the /categories and not in the root.
The solution i give is:
1. name your routes for easier use across the site
Route::get('/jaarhoroscoop', [
'as' => 'jaarhoroscoop.show',
'uses' => 'JaarhoroscoopController#index'
]);
2. In your view or your nav view make the links like:
Jaarhoroscoop
I have added a parameter but you can avoid it in your case.
Edited:
If you don't want to use a controller:
Route::get('/jaarhoroscoop', function () {
return view('jaarhoroscoop');
})->name('jaarhoroscoop.show');
Laravel also has a helper function called url which can generate absolute urls instead of using relative urls which tend to create problems like the one faced by you. All you need to do is this (assuming you are using blade for nav file as well)
<a class="headnavitem" href="{{ url('jaarhoroscoop') }}">Jaarhoroscoop</a>
else for vanilla php file you can do:
<a class="headnavitem" href="<?php echo url('jaarhoroscoop') ?>">Jaarhoroscoop</a>
This will always generate the output:
<a class="headnavitem" href="your_website.com/jaarhoroscoop">Jaarhoroscoop</a>

zend-framework problem using routes

UPDATE:
Turns out it's not only the routing, but any instance of going 'up' a directory.
For example mysite.com/public serves me the correct index.phtml file...
mysite.com/index/testpage DOES serve me the testpage.phtml, but my directory structure is completely screwed and I lose all links to images & Stylesheets etc. I've checked my .htaccess and it's exactly as recommended.
Serious head-scratching time here...
OP:
Hello..
I'm using this method for catching urls and serving up the correct content
$router->addRoute(
'list',
new Zend_Controller_Router_Route('/list', array('controller'=>'index', 'action'=> 'list'))
);
And it works really well, serves up the content from list.phtml...
but, the moment I attempt anything 'up' a directory such as this I lose all my style sheets and other relative scripts.
$router->addRoute(
'listWithUsers',
new Zend_Controller_Router_Route('/list/:users', array('controller'=>'index', 'action'=> 'list'))
);
It's strange because I can access the users variable and echo it to screen, it's just that I lose all scripts and stylesheets whenever a URL contains /
For clarity, I'm able to set actions in the indexController.php to catch URLS and it works, but it seems to serve it as another directory causing me to lose all relative links.
Thanks for any help. Please advise if any specific details would help.
For any kind of MVC app using routes like this, you have to specify absolute path for your resources. This will prevent the issue you have.
For example, if I have a website located at mysite.com/app1 and I have a resource located in lib/jquery-1.2.6.min.js, then a valid path will be /app1/lib/jquery-1.2.6.min.js.
For this I have always used some kind of helper to return the site's base URL in my views.
<script type="text/javascript" src="<?php echo $this->baseUrl() ?>/lib/jquery-1.2.6.min.js"></script>
You can use a view helper or a constant.

Categories