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>
Related
I am building a social networking application.
In various pages such as the "profile", "dashboard" and "trending" pages I wish to display multiple posts.
I have made a post.blade.php file in my resources/views/includes folder.
I have included this file in all pages that require it like so:
#foreach ($posts as $post)
#include('includes.post', ['post' => $post])
#endforeach
#section('myJavascriptCode')
<script>
// I want to paste in the Javascript code necessary for "includes.post" here
</script>
#endsection
My guess is that I should write a corresponding post.js file and include this script in each of my pages that displays posts. However this would be tedious to maintain parallelism between my blade template files and their corresponding js files.
<script src="post.js"></script>
If this is the case,in which directory should I store my js? I would imagine in a resources/js/includes directory which mimics the path where my post.blade.php file is stored.
Store your js file in your laravel public folder and call it this way to use
<script src="{{asset('post.js')}}"></script>
this work perfectly
check this link : https://laravel.com/docs/8.x/blade#stacks
you can use #stack and #push to import script to parent from child.
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.
In the application I built I have a view called main (http://127.0.0.1:8000/main). Following is how I load the view file from Route file, web.php.
Route::resources(['/main' => 'pages\MainController']);
Above loads the 'main.blade.php' view controller from the index function of MainController.php file.
public function index() {
return view('pages\'main');
}
My problem is, I have an edit button in the view which the href value is set as following.
<a href="main/{{$data->id}}/edit" >Edit</a>
Click of the above will direct me to 'http://127.0.0.1:8000/main/1/edit' route
and this route will not load any styles and javascript files like it was loaded in 'http://127.0.0.1:8000/main' . What is the work around to retain the resource styles and js functionality ?
Following is one example I have linked my resource css in my main layout blade
<link href="vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet
You need to include the resource URLs within url php function.
{{url('vendors/font-awesome/css/font-awesome.min.css')}}
So the complete code is.
<link href="{{url('vendors/font-awesome/css/font-awesome.min.css')}}" rel="stylesheet">
href="{{ URL :: asset ( ' your css path ') }}"
Make sure you css is in public directory
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
Is it possible to get the location of a view that is passed into a Laravel view composer?
View::composer('*', function($view) {
// I want to find out the location of the view file here
// e.g. master.something.header
// then add this to an array
$loadedViews = View::share("loadedViews");
$loadedViews[] = $thisViewName;
});
The reason is that I want to have a variable that will be shared between views and contain an array of all the views that are loaded. Any css and js files will be located in directory structure that matches the views one.
This means I can then have a css and js view which then include the required css and js files for the views on the page. All css and js will be directly linked to a specific view.
If there is already a way to do this, or a way to get a list of loaded views, please let me know!
$view->getName() was the answer that I wanted.
$view->getPath() is the actual path to the file.