accessing lang variables from controller + codeigniter - codeigniter

How can I access my lang variables in controller?
function index()
{
$seo_title = $this->lang->line('blablabla');
$data['page_title'] = $seo_title;
$this->load->view('contact/index.php',$data);
}
in my lang file blablabla has string in, and it works well when I call from view file. but when I want to call from controller, it doesnt return anything :(
any idea about problem? appreciate!!

You need to load the language file you want to use before you grab lines from it:
$this->lang->load('filename', 'language');

Related

Passing variable to .blade.php by return on Controller

I'm trying to pass a varible from the Controller to my .blade.php file.
I'm returning the view and compacted variables to the .blade.php but it doens't recognize the
variable.
This is the code of the Controller.
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison')->with(compact('contents'),$contents)->with(compact('contents2'),$contents2);
And i'm trying every way just to get an result but instead i'm getting the "Undefined variable $contents" page. The last method i used was a simple
<p>{{$contents}}</p>
I don't think it's correct but i don't really remember how to do it.
In controller return like:
return view('comparison.comparison', compact(['contents', 'contents2']);
And make sure your file is in resources/views/comparison/comparison.blade.php
Try this
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison', compact('contents','contents2'));
if you have defined a veriable above just use tha name inside compact as above and it can be acced inside blade as <p>{{$contents}}</p>
You can pass variables like that it's not mandatory to use compact.
return view('comparison.comparison', [
'contents' => $contents,
'contents2' => $contents2
]);
or if you want with compact:
return view('comparison.comparison')->with(compact('contents', 'contents1'));

How to include a variable in a laravel redirect

I have a variable in a Laravel 5 controller which I am trying to include in the url of a redirect link, however nothing I've tried seems to work, my code is currently as follows.
$newChallenge = ((int) request('challenge_id') + 2);
return redirect('/texttext/{{$newChallenge}}');
However, this doesn't use the variable represented by $newChallenge, but rather
"{{newChallenge}}" as a string, how should this be done?
{{ }} is meant for use in blade files.
I think you can do return redirect('/texttext/{ $newChallenge }'); if you really want those { } brackets.
Or just return redirect(sprintf('/texttext/%s'), $newChallenge));
or return redirect('/texttext/'.$newChallenge);
Edit: Also, your redirect could probably benefit from being a named route. https://laravel.com/docs/5.8/routing#named-routes
May be you can change it to a query parameter
like
return redirect('/texttext/?=newChallenge={{$newChallenge}}');
if you want to use
return redirect('/texttext/{{$newChallenge}}');
It should be something like below
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge');
and newChallenge signature will function newChallenge(Request $request, $newChallenge)
if you want to use something like return redirect(route('new-challenge', $newChallenge));
just add name to route like
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge')->name('new-challenge');
I have not tested, but I think it should work.

Laravel pass data between views

I have a view with a few variables that I want to pass to a new page and prepopulate a form with them.
$data->title
$data->catid
$data->category
$data->groupName
The "vanila way I could make a url like something.com?var1=something&var2=something etc.
But how can I do this in laravel?
Considering you can redirect to a page with GET parameter. For example, "something.com?var1=something&var2=something", You can actually pass this data to another view in Laravel. Let's assume you want to pass var1 and var2 variables to another view.
You will redirect user to the URL containing two variables as GET parameters, You will also execute Controller function that will handle that route. Let's assume that following is your web.php or routes.php file.
Route::get('firstpage","SomeController#view1");
Route::get("secondpage","SomeController#view2");
Now, in view2 method of the SomeController, You can check if those variables exist or not. Here is an example of var1 and var2.
public function view2()
{
if(isset($_GET['var1'])){
$var1 = $_GET['var1'];
}
// Similar for var2
}
This is how you can pass variables between views.
The best way IMHO is using sessions.
session()->put('data', $data);
And to retrieve it and to check if the key exists:
if (session()->has('data')) {
$data = session('data'); //array
}
If you use the $data in session() only once you can pull it session()->pull('data'); //array
A word of caution if you use database sessions. You may have to change the field 'payload' from TEXT to MEDIUMTEXT if $data holds larger amounts of data.
The use of sessions is not search engine (SEO) friendly. If that matters, the easiest way to make the url is $str = http_build_query($data); and to build the url \URL()->route('your.route').'?'.$str;
To access the data from the URL: $title = \Request::query('title');

Code igniter url segment

currently my website url is
https://www.thefoolsbookie.com/main/inside?id=8
but I want this to be like this
https://www.thefoolsbookie.com/nfl
How can I do this?
Edit the application/config/routes.php file and add a new route for it
$route['nfl'] = 'main/inside';
It should be as simple as that :)
See https://ellislab.com/codeigniter/user-guide/general/routing.html for more examples.
Edit
I've worked with CI for a long time and I've never seen it use GET params in the routes file in that way. I'm not sure it is possible.
You could have it like $route['nfl'] = 'main/inside/8';
then in the main controller your inside method would look like:
public function inside($id)
{
//$id would contain the ID of 8 when you go to
//https://www.thefoolsbookie.com/main/inside/8
//or https://www.thefoolsbookie.com/nfl
}

Calling string in separate file in CodeIgniter

I'm building a profanity/racial slur filter for my website. I have it working, but my preg_match string is quite long. I'm just wondering if there is some way to host this long string in a separate file in CodeIgniter and then call it when I need to in the preg_match.
I have googled this and I couldn't find anything, so I thought I would ask here.
What I'm doing now is hosting my string in the model and then calling this:
if(preg_match($filterRegex)){
databaseStuffHere();
}
Here are a few options. Depending on how and where you are using this string and function, one may be better than the others.
Config
You could store the value as a config, in application/config/config.php
$config['filter_regex'] = 'yourReallyLongString';
The primary config is auto-loaded by CodeIgniter, so you can use it like so:
$filterRegex = $this->config->item('filter_regex');
if(preg_match($filterRegex, $subject))
{
databaseStuffHere();
}
Constant
If you're using this long string in several places and it would be useful to have global access, you could define it as as constant in application/config/constants.php. It will also prevent you from accidentally redefining the value.
define('FILTER_REGEX', 'yourReallyLongString');
Then use it with your function like this:
$filterRegex = FILTER_REGEX;
if(preg_match($filterRegex, $subject))
{
databaseStuffHere();
}
Helper
Finally, you could use a helper. You can load the helper when required, or auto-load it. You can create your own helper in application/helpers/. It could look something like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('filter_slurs'))
{
function filter_slurs($subject = '')
{
$filter_regex = 'yourReallyLongString';
if (preg_match($filter_regex, $subject))
{
return FALSE;
}
else
{
return TRUE;
}
}
}
Having a function to handle this may make your code easier to follow and more meaningful, for example, in your controller, you could use it like this:
$this->load->helper('slur_filter_helper'); //assumes the helper file is: slur_filter_helper.php
if(filter_slurs($subject))
{
//do something
}
else
{
//do something else
}
You can use it with config file (system/application/config/config.php) to set configuration related variables.
====================== DEFINE WHAT YOU WANT in config.php ===========================
$config['REQUIRED_SRTING'] = 'YOUR_REQUIRED_LONG_STRING_OR_WHAT_YOU_WANT_STORE';
But best place to set constant is
(system/application/config/constants.php) to store site preference constants.
===================DEFINE WHAT YOU WANT in constants.php=========================
define('CONSTANT_STRING','YOUR_REQUIRED_LONG_STRING_OR_WHAT_YOU_WANT_STORE');

Resources