Silex and Twig: global.request.baseUrl is empty - php

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

Related

Lumen's url() function performing strangely

I recently did a fresh install of Lumen framework and started building a site from it. Yes I know lumen is designed only for APIs but please help me out with this.
I have placed all my views inside /resources/views and my templates inside /resources/views/templates.
Now since I had to place [css/js/images] somewhere, I thought placing all of that in public/assets/[css/js/images] would be nice.
Now in my layout when I am trying to include css - I use something like this -
<link href="{{ url('assets/css/something.css') }}">
and it works giving an output of something like this -
<link href="localhost/assets/css/something.css">
same thing works for js also but it gets strange when I try to include images. For including an image I write something like -
<img src ="{{ url('assets/images/someimage.jpg') }}"
and when I view page source, output is as I expect it to be -
<img src="localhost/assets/images/someimage.jpg">
but my console fires 404 not found errors stating someimage.jpg not found. And when I crosscheck by inspecting the image's parent, the Url is totally different soemthing like this -
<img src="localhost/images/someimage.jpg">
See, automatically omitting 'assets' from image url and resulting in 404, but I can see correct url when I view page source.
Things I tried to resolve the issue -
Cleared cache and reloaded the page.
Tried using asset() instead of url() but prototype of that was removed from lumen.
Pull out [css/js/images] folder from assets and pasted them in parent i.e. public. This worked but then the question is why did the previous setup worked find for both css and js and caused problem only with images ?
My other questions are -
1. How can the url in page source be different from the one being rendered ? Just to mention in my case the url in page source worked and displayed image but since the url being renderred omitted 'assets' from path hence resulted in 404.
2. Is there any other good way to include these assets in views. If yes then please also mention where to put them ?
Attaching some images/code for reference.
Output of rendered page showing 404 errors for images but none for css.
Output of view page source windows showing asset included in image path
No clue if this is right, but I believe you actually need to put an image stream inside that URL. Now the server is trying to retrieve some byte encoded object that isn't there.
I have no idea if it's the same case for you, but I've had many instances where I had to put in image streams instead of URLs, which I've solved this way using this library:
Routes.php
/**
* Image handling routes.
* These make sure images actually are images instead of bytecode
*/
Route::get('organization/logo/{logo}', function($logo) {
$file = Image::make(Storage::disk('logo-image')->get($logo));
return $file->response();
});
View
<img src="{{ asset('organization/logo/' . $organization->logo_path) }}" alt="">
I might be completely off, but I recognize what's happening with your app and this took care of the issues when I implemented it.
Check your web server configuration. It sounds like you have some type of redirect setup that redirects assets/images/* to just images/*.
As a simple test, open your "Network" tab and navigate your browser to http://samplelumena.local/assets/images/footer1.jpg. I'm guessing the Network trace will show a 30x (301, 302, etc.) to http://samplelumena.local/images/footer1.jpg, followed by the 404 for that url.

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>

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.

generating url not based off subdomain's in twig

I have followed the following tutorial to handle dynamic subdomains. So say the redirection is successful and I am already at www.subdomain.domain.com. Now when I click on other links in the site it goes to www.subdomain.domain.com/myotherlink, while in fact all of my links should go to www.domain.com/myotherlink. Here's how I generate links in my twig file:
{{ path('MyAppMainBundle_marketplace') }}
The route in the controller looks something like this:
/**
* #Route("", name="MyAppMainBundle_marketplace", options={"sitemap" = true})
* #Template("MyAppMainBundle:MarketPlace:index.html.twig")
*/
How do I enforce such that all links on my site that is generated using the twig syntax above goes to mydomain.com/myotherlink? I think the issue is I don't want a relative path. I wanted an absolute path? I tried replacing path above with url, however it didn't work
A little more information on exactly what the template you are using is creating the link in question, but I think this other stackoverflow answer might help:
https://stackoverflow.com/a/9750494/1978759
Hope this helps.

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