LARAVEL 5.5 AND UP
Use the full form of the blade directive:
@php
$i = 1
@endphp
LARAVEL 5.2 - 5.4
You can use the inline tags:
@php ($i = 1)
Or you can use it in a block statement:
@php
$i = 1
@endphp
ADD A 'DEFINE' TAG
If you want to use custom tags and use a @define instead of @php, extend Blade like this:
/*
|--------------------------------------------------------------------------
| Extend blade so we can define a variable
| <code>
| @define $variable = "whatever"
| </code>
|--------------------------------------------------------------------------
*/
\Blade::extend(function($value) {
return preg_replace('/\@define(.+)/', '<?php ${1}; ?>', $value);
});
Then do one of the following:
Quick solution: If you are lazy, just put the code in the boot() function of the AppServiceProvider.php.
Nicer solution:
Create an own service provider. See https://stackoverflow.com/a/28641054/2169147 on how to extend blade in Laravel 5. It's a bit more work this way, but a good exercise on how to use Providers :)
LARAVEL 4
You can just put the above code on the bottom of app/start/global.php (or any other place if you feel that is better).
After the above changes, you can use:
@define $i = 1
to define a variable.
In Laravel 4 & 5 (up to 5.7), you can use str_limit
, which limits the number of characters in a string.
While in Laravel 5.8 up, you can use the Str::limit
helper.
//For Laravel 4 to Laravel 5.5
{{ str_limit($string, $limit = 150, $end = '...') }}
//For Laravel 5.5 upwards
{{ \Illuminate\Support\Str::limit($string, 150, $end='...') }}
For more Laravel helper functions http://laravel.com/docs/helpers#strings
Best Answer
You can use:
Request::url()
to obtain the current URL, here is an example:Laravel offers a method to find out, whether the URL matches a pattern or not
Check the related documentation to obtain different request information: http://laravel.com/docs/requests#request-information