I am writing my own CMS using Symfony 3 right now and i have problem with include the same variables in all controllers.
For example:
I want render logo which i have just upload in my admin panel so i keep URL to it in database and fetch with other data like meta (site descryption, site title etc.) in controller - passing it as array and call in base.html.twig
However base.html.twig extends all other twig templates which i use in controllers so i must fetch it in all of them.
Is there some nice solution for my problem ?
Posting an answer based on your comment.
Use addGlobal() method in your base controller like so:
$this->get('twig')->addGlobal('variable', $variable);
Where $variable is the global variable you need.
Related
Twig has this awesome feature called Twig Filters which allow you to change the variables that you send to your view, from within the view without messing with the data model.
{ variable_name | filter_name }
This makes it super-duper readable and clean. Filters can be lowercase, encoding, raw text or you can build your own.
Question is simple: I really miss this kind of functionality, what's the best way to implement something like this in Laravel using Blade?
as i know Blade don't offer this functionality of filters, but u can create a helper file that can has many functions helper and every function has her logic.
in this link u can find how to create a helper file with functions and call them inside ur blade file tutorial how to create helper function
I have a Controller that parses an XML and returns a view with a list of names and URLs.
return view('view_1',compact('myList'));
View_1 will have a form with parameters method="POST" action="goToView_2"
Then I get some information from my view_2 through a POST, but I still want to keep $myList so that view_2 view uses it aswell.
How do I pass $myList from the first view to the next through a controller?
It sounds like you're trying to have multi-step form of some kind.
I would store the data in the session and easily access it in the second controller or in the view (although not recommended).
https://laravel.com/docs/5.4/session#using-the-session
PS. I personally love using the global session helper.
In my view I create links via:
URL::action('NotSureWhatController#getIndex', 'id') }}
My view is a template that is used by a variety of different controllers, what's the best way to change the name of the controller in the action?
The only thing I can think of is setting a var in the controller and passing it through.
Is there a better way? Or a way to get the controller name?
I can't use 'as' in the route to name the controller either (as this is used for something else) so this won't work:
Route::currentRouteName()
Option 1
You should create the URL directly in the controller, then pass it as a variable to the view. The view will just print the url.
Option 2
You pass the name of the controller as a variable to the view (always from the controller), then you use the escape values of the blade templating to print it inside your function to generate URLs.
Option 3
Using the REQUEST class to get information about the page.
How to fetch data from controller to views in CakePHP or Yii but not embed PHP code in views file (view is html file).
it similar Template Parser Class in Codeigniter:
https://www.codeigniter.com/user_guide/libraries/parser.html
You will need to load the data in the action controller by calling one of the model functions like findAll()
here is an example, assume we have a User model, and you need to pull out all the data and render them at view file:
public function actionIndex(){
$model = Users::model()->findAll();
$this->render('index', array('model' => model));
//the model variable will be defined in the index.php
//file and will have a value as assigned in the array
//index.php file should be located under /views/users/index.php
}
Now in index.php file do a var_dump and see what results you got.
I don't see the problem with embedding php into HTML. But if you really don't want to do it, you can use CakePHP with a template library like Mustache.
Check this: https://github.com/electblake/CakePHP-Mustache-Plugin
I am trying to implement multi-language URLs. Thus I want to have URLs like:
/de/ueber-uns/kontakt and /en/about-us/contact
So far so good, I use App::before() in filters.php to check the locale given. I think I then need a route in routes.php for every controller action in every language.
So I thought of dynamically creating the file routes.php. All I would need for it is to know how I can access all available controllers or get all registered routes in code (like artisan routes but not with CLI).
So the questions are:
is the general approach for multilingual urls correct?
is it possible to access all controllers to extract the methods somehow?
how could I get the RouteCollection that is used within \Illuminate\Routing\Router.php?
Thank you in advance!
I ended up doing the following:
1) Routes in routes.php are dynamically created with a custom artisan command. It parses all Controllers and creates routes for every action in every language that is supported. The language string is handled with routes like
Route::get('{lang}/customer/login', 'CustomerController#getLogin')->where('lang', '[a-z]{2}').
This way users can just change the language string and the site will load in the correct language (if supported).
Routes for different languages all lead to the same controller action. For these languages except english, I need translations (routes.php in /app/lang).
2) a before filter for those controllers whose actions get translated is set in constructor. It basically checks if the language string is valid and replaces it if not. The chosen language will be set in the session.
I hope anybody can use it :)