Laravel Jquery: URLs are being read as " - php

I'm making an app with Laravel. I have a variable called dataActivity, and I want to use this as an image url. Since Laravel uses {{ URL::asset('path') }} to make a link, I did this:
$('.box').css("background-image", "url('{{ URL::asset('icons/"+dataActivity+".png')}}')");
However, the not found error outputs the link as http://localhost:8000/pictures/"+dataActivity+".png
As you can see, there are &quots where there shouldn't be. Any idea how to fix this?

use
$('.box').css("background-image", "url('{!! URL::asset('icons/"+dataActivity+".png')!!}')");
instead of
$('.box').css("background-image", "url('{{ URL::asset('icons/"+dataActivity+".png')}}')");

Related

How to get last part of url in blade file (HTML) using laravel 5.2?

I am new to laravel.
I want to get the last part of my url in the blade file(HTML file).
I have done this one using php functionality .
Is there any way I can get it using any laravel functionality .
Below is my code ,its working fine
<?php
$url = url()->current();
echo $end = end((explode('/', $url)));
?>
I have also used this one to get
Request::segment(2)
Here my url is http://localhost/blog/public/user/add-user.
I want to get add-user in the html file.
Thank you
Try this code for getting last value of URL
$uri_path = $_SERVER['REQUEST_URI'];
$uri_parts = explode('/', $uri_path);
$request_url = end($uri_parts);
The answer to your question is: "No there is not any Laravel functionality" or more precisely Laravel helpers for getting the last segment, but fortunately you can just use php. The end has been mentioned and end works on an array, but I don't think it is possible to use directly on Request::segments(). That means you have to put Request::segments() into a variable (like $lastSegment) first and then use end on the variable.
The best and slimmest way I could find to do this is:
{{ basename(Request::url()) }}
Use this in blade, but of course like mentioned before you can add this in the controller or a Laravel view composer.
{{last(request()->segments())}}
If you specifically want the last segment (it may be the 2nd, 3rd, 4th etc) you can use end(Request::segments()) to conistently always get the last segment.
Also, if you don't want to call functions in your templates and be a bit more 'Laravel' why not bind it to your view from the controller?
In your controller:
public function show()
{
return view('your.view')->with('lastSegment', end(Request::segments()));
}
Then in your view you can do this:
<p> The last segment is {{$lastSegment}} </p>
Also, if you want to have $lastSegment be available in all your views without having to code it in all your controllers, look into using Laravel's view composer. Very powerful for making templates.

How to print query result in python/django

I came from CakePHP and just started playing with Django framework. In CakePHP, I have the habit of printing out all the returned array using pr() straight out to the webpage. For example:
A controller spits out a $result to a View, I use pr($result) and it will print out everything right on the webpage so I know how to travel through $result from my View.
A form POST a $request to a Controller, I use pr($request) to see what is sending in before processing it in the Controller. The content of $request will be displayed immediately on the webpage right after I hit Submit the form.
I'm wondering if I could do the same thing in django instead of going to the shell and try pprint (or could I just use pprint to print out to the web???)
This is a really simple example about what I'm talking about:
app_name/views.py:
def detail(request, soc_id):
soc = get_object_or_404(Soc, pk=soc_id)
return render(request, 'socs/detail.html', {'soc': soc})
How can I just view clearly what is in "soc". In cakephp, I could just pr($soc) right there and it will be displayed right on the detail.html page.
I have tried this and it didn't work (I'm sure it's basic but i'm just new to this)
import pprint
def detail(request, soc_id):
soc = get_object_or_404(Soc, pk=soc_id)
pprint.pprint(soc)
return render(request, 'socs/detail.html', {'soc': soc})
I have spent two days doing research but I couldn't find the answer. I'm hoping one of you can help this newbie out.
The way you're trying to print will show the print in the terminal that is running your Django server. If you just need to see it quick, check there.
If you want to output the value on to the rendered page, you'll have to include it in the template using template tages. It looks like you send {'soc': soc} to your template as context. Because of this, you should be able to use it in your template. So, in your template (socs/detail.html), just add {{ soc }} somewhere and it should print out the value. The template has full access to the object, so if you wanted something specific, you can also print that instead (like {{ soc.id }}).
If you want to see everything that's in the object without specifying all of the different fields yourself, send OBJECT.__dir__. For example, for this you'd send {'soc': soc.__dir__} as your context. Keep in mind that this likely should not be used for anything but inspection on your part.
If you'd like to learn more about Django's templating, check out the syntax and more.

PHP: Sending a request and getting a response

I'm from ASP.NET MVC background and this is first time I'm trying to write something in PHP.
In ASP.NET MVC we can develop models for our data and using the actions that we write we can get them or send them to another action. What I mean is that
public ActionResult Login_Action(LoginModel _Model) {
// Authenticating the user
return RedirectToAction(X);
}
when calling this the url that is shown in the address bar (in case of using GET, if it is POST nothing will be shown after the page name) will be:
www.WebsiteX.com/Login?Username=something&Password=something
The problem is that I don't even know how search for this in google (like by typing what exactly) because in Microsoft side, these are handled automatically the way I described.
But in case of PHP, how can I get the values in the address bar? do I have to get the actual address and then break the values down into arrays?
I'd appreciate any help.
First of all, this seems to be invalid for me: www.WebsiteX.com/Login?Username=something?Password=something The first parameter need to be ? and the others should be &.
Second: You can get your values of your parameters by accessing the $_GET global array.
Eg. for the username echo $_GET["Username"];
Are you using any framework? You should. And then, the Framework will give you the way to do that. In ASP.NET you use a Framework so do the same in PHP.
With vanille PHP you can get the GET values with $_GET['Username']. But please, use a framework.
I think that the most popular are Laravel and Symfony right now.
Example:
In laravel you can bind a parameter to a variable so you can do something like:
//Url: mywebsite.com/user/1/
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
Which is similar with the ASP.NET example.

Laravel 3 routing issue

I came from Codeigniter. In CI I had this:
$route['([a-z]+)tab'] = "$1/tab";
When I go to index.php/sometab/ I'll get some/tab/ action executed. But it doesn't redirect, instead I just tell CI that when I type this address I want to use another address instead, though there is no redirect.
Basically I want to achieve next goal: when I go to /someTab/ I want to execute some#tab action.
I only found Redirect::to_action in laravel, but I dont want the URL to be changed. I tried something like:
Route::any('([a-z]+)tab', function($controllerName) {
return Redirect::to_action("{$controllerName}/tab"); // here I want to tell to use $controllerName#tab action
});
How can I get this?
http://laravel.com/api/class-Laravel.Routing.Controller.html
There are some interesting methods here to. Route::cal, Route::forward, Route::execute...

Twig templates engine: get current url

How can I get the current URL from a Twig template?
I am using Twig with PHP, without any other framework.
The following works in Silex and most certainly in Symfony2 as they share the Request class (I did not test though) :
{{ app.request.getRequestUri() }}
Finding the current URL
The current URL is supplied by your web server and written to the $_SERVER super-global. Run this small script, <?php echo '<pre>'; print_r($_SERVER);, through your server and root around to find the value(s) you are looking for.
Related questions on this subject:
getting current URL
PHP Determining the current url
How to get URL of current page in PHP
The PHP manual describes the nature of the available $_SERVER values here.
Getting the URL in TWIG
After you have the URL, you need to pass it as a template variable when calling render(...) on the Twig template instance. For example, you might code this.
$current_url = // figure out what the current url is
// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));
To use the variable in the template, you use the {{ variable_name }} syntax.
Developer's basic Twig documentation
Twig designer documentation for using variables.
Go http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
or : {{ app.request.getUri() }} for full Uri.
Keeping best practice in mind, at this time you should use Symfony\Component\HttpFoundation\RequestStack.
See http://symfony.com/blog/new-in-symfony-2-4-the-request-stack.
As of Symfony 2.4, the best practice is to never inject the request service, but to inject the request_stack service instead [...]
So in a Silex application it could be achieved with :
app.request_stack.getCurrentRequest.getUri
Here something I found to make it generic with the sliex Framework.
I guess my solution is not perfect but it's get the job done.
in your PHP code add this code :
$app = new Silex\Application();
// add the current url to the app object.
$app['current_url'] = $_SERVER['REQUEST_URI'];
Then in your Twig template you can do
{{ app.current_url }}
Let me know what is the botom line of this method.

Categories