Assume this url:
http://localhost/myproject/public/add/11
The result of this code: echo $request->path() is add/11. Now I want to know how can I get that value in pure php?
Because I need to use that value in a view which isn't called by a controller (it is a master view).
Since you're using blade, try this:
{{ request()->path() }}
Since it's a Blade view, you can use Request facade:
Request::path()
Or global helper:
request()->path();
$path_only = parse_url('http://localhost/myproject/public/add/11', PHP_URL_PATH)
echo $path_only;
and result :
/myproject/public/add/11
Related
This is my code in Laravel :`
Route::view('/welcome/{name}','welcome',['name'=>'name']);
I want to know is it possible get and pass {name} parameter to view by Route::view or not?
I know that we can do it with Route::get :
Route::get('/welcome/{name}', function($name){
return view('welcome',['name'=>$name]);
});
Instead of trying to pass parameter use the segment. Try this in your view:
{{ Request::segment(2) }}
I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.
I used to use smarty a lot and now moved on to Laravel but I'm missing something that was really useful. The modification in the template of you're variable.
Let say I have a variable assign as {{$var}}. Is there a way in Laravel to set it to upper case ? Something like: {{$var|upper}}
I sadly haven't found any documentation on it.
Only first character :
You could use UCFirst, a PHP function
{{ucfirst(trans('text.blabla'))}}
For the doc : http://php.net/manual/en/function.ucfirst.php
Whole word
Str::upper($value)
Also this page might have handy things : http://cheats.jesse-obrien.ca/
PHP Native functions can be used here
{{ strtoupper($currency) }}
{{ ucfirst($interval) }}
Tested OK
You can also create a custom Blade directive within the AppServiceProvider.php
Example:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
Don't forget to import Blade at the top of the AppServiceProvider
use Illuminate\Support\Facades\Blade;
Then use it like this within your blade template:
#lang_u('index.section_h2')
Source:
How to capitalize first letter in Laravel Blade
For further information:
https://laravel.com/docs/5.8/blade#extending-blade
works double quoted:
{{ucfirst(trans("$var"))}}
I'm trying to get a url parameter from a view file.
I have this url:
http://locahost:8000/example?a=10
and a view file named example.blade.php.
From the controller I can get the parameter a with $request->input('a').
Is there a way to get such parameter from the view (without having to pass it from the controller to the view)?
This works well:
{{ app('request')->input('a') }}
Where a is the url parameter.
See more here: http://blog.netgloo.com/2015/07/17/lumen-getting-current-url-parameter-within-a-blade-view/
The shortest way i have used
{{ Request::get('a') }}
Given your URL:
http://locahost:8000/example?a=10
The best way that I have found to get the value for 'a' and display it on the page is to use the following:
{{ request()->get('a') }}
However, if you want to use it within an if statement, you could use:
#if( request()->get('a') )
<script>console.log('hello')</script>
#endif
More simple in Laravel 5.7 and 5.8
{{ Request()->parameter }}
Laravel 5.8
{{ request()->a }}
This works fine for me:
{{ app('request')->input('a') }}
Ex: to get pagination param on blade view:
{{ app('request')->input('page') }}
You can publicly expose Input facade via an alias in config/app.php:
'aliases' => [
...
'Input' => Illuminate\Support\Facades\Input::class,
]
And access url $_GET parameter values using the facade directly inside Blade view/template:
{{ Input::get('a') }}
As per official 5.8 docs:
The request() function returns the current request instance or obtains an input item:
$request = request();
$value = request('key', $default);
Docs
All the answers above are correct, but there's a quickier way to do this.
{{request("a")}}
Laravel 5.6:
{{ Request::query('parameter') }}
As per official documentation 8.x
We use the helper request
The request function returns the current request instance or obtains
an input field's value from the current request:
$request = request();
$value = request('key', $default);
the value of request is an array you can simply retrieve your input using the input key as follow
$id = request()->id; //for http://locahost:8000/example?id=10
if you use route and pass paramater use this code in your blade file
{{dd(request()->route()->parameters)}}
here is the code to get filtered data with pagination
$queryvariable->appends($data)->links('link of new page');
note there
$data = $request->all();
{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0