Laravel lang resetting to ENGLISH with POST route - laravel

So here is my problem, I'm able to use Localization with Laravel. I've been using it on GET route, the problem occur with my POST route. For example, I'm in french on my registration page, when I click on register, once I enter my controller, the language is reset to english, but it must not be reset, because my controller is supposed to return a view with a text, so this view is supposed to be in the right language. I tought I could pass the lang through URL parameter, but since it is a POST I cannot and I must not use GET because it's a registration form. The thing is in my route I set the lang to what the lang is already, since it is already in french, from when I accessed the registration page, it should something like lang = 'fr', but no the program act like each time it set the lang to french, to second after it reset it to english. I've been stuck on this for a while and I cannot see the solution, can someone help me ?
Here is my 'french' registration form:
Here is the route 'register' (When you click on register/inscription):
Here is a part of my controller:
Here is the view my controller returns (It is supposed to be written 'fr' not 'en'):
Thanks in advance for your help and your time !

I've found the solution, now when I enter a page like to registration page I do this
And in the function that is triggered when clicking register/inscription I added this at the beginning

Related

Is there a way to set optional route location parts?

firstly, I am not sure how to structure the question title correctly, feel free to edit it to represent the question correctly. I will try to explain what I mean.
I implemented localization on my app (URL of tutorial is at bottom of this post), and I have created my route as follows:
// Set language
Route::get('lang/{locale}', 'PagesController#lang');
And it works. But only on pages which have only one "level" in the url:
example.com/first-level - changing language works on these URLs
example.com/first-level/second-level - changing language doesn't work on these pages
At first I thought the problem is with url parameters, cause by chance, the pages where I first noticed changing language didn't work was on a url which has a route with an parameter like this
example.com/first-level/{id}
So then I tried to rewrite the route for changing languages to this (added an optional parameter)
// Set language
Route::get('lang/{optional?}/{locale}', 'PagesController#lang');
But that didn't work either
The language is changed by anchor tags which point to the route
<a href="lang/en">English</
Serbian
Expected result: being able to change the language from whichever page the links are clicked. Clicking the links on pages like example.com/one/two and example.com/one/{id}/two are supposed to work.
Actual results: changing language works only on example.com and pages like example.com/one , and fails on example.com/one/two and example.com/one/{id}/two . When the links are clicked on these pages, the app is sent to an URL like this: example.com/one/lang/en and example.com/one/{id}/lang/en respectively.
I implemented localizations on my project following this tutorial https://appdividend.com/2019/04/01/how-to-create-multilingual-website-using-laravel-localization/#Step_2_Creating_Translation_Files
You can simply define both routes.
Route::get('lang/{locale}', 'PagesController#locale');
Route::get('lang/{optional}/{locale}', 'PagesController#lang');
Then handle the optional variable with your controller methods.
public function locale($locale)
{
return $this->lang(null, $locale);
}
public function lang($optional, $locale)
{
// ... your logic
}
As for your link problem, you need to use an absolute path.
// relative path — https://yourdomain.com/current/path/lang/en
English
Serbian
// absolute path — https://yourdomain.com/lang/en
English
Serbian

Can't link to an anchor on another page in codeigniter

From the research I've done this code should work. But for some reason I keep getting the error that the page is not found. Maybe it's because it's going though my Main_controller?
public function privacy($page='privacy') {
$this->load->view($page);
}
The VIEW:
Privacy Policy<span class=""></span>
The idea is have the privacy popup from anywhere I would want to link to it. Right now I have to put the text on the same page and then link to it through the id="myModal-privacy".
Thanks for any and all input.
The codeigniter url convention is: "mysite.com/controller/function/id. see docs
your link contains a hashtag, which is not working as id like you intend, change it to the CI-convention like this:
Privacy Policy<span class=""></span>
which will now send in the url myModal-privacy as id and you'd load this page in the controller:
$this->load->view('myModal-privacy');
In your question controller name is Main_Controller and in the view, you are using main.
If you are not defining route then use same controller name to call the function
Privacy Policy<span class=""></span>
Did you forget to add a question mark?
Correct Code
Privacy Policy<span class=""></span>

Laravel Redirect as POST

Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });
POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.
You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.
For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.
return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])

CodeIgniter jQueryUI dialog form example

I am trying to use CodeIgniter and jQuery-ui dialog to create a modal window with form to update user information.
The process should be like:
1. Press a button on a view page.
2. A modal window pops up.
3. Inside the window is a form that a user can fill.
4. If the user filled something before, the information should be shown in corresponding field
5. Click the update button on the modal window to save the changes to database.
Can anyone provide a good sample of this process?
I used ajax to pass the data but it didn't work when I was trying to update the data to the database. It would be nice if an example of how to pass data from ajax to php and how php handle that.
Thanks,
Milo
well the jquery bit for post(), get(), ajax() works the same in any measure you would normally use it.. key difference here is with CI you can't post directly to a file-name file-location due to how it handles the URI requests. That said your post URL would be the similar to how you would access a view file normally otherwise
ie: /viewName/functionName (how you've done it with controllers to view all along. post, get, ajax doesnt have to end in a extension. I wish I had a better example then this but I can't seem to find one at the moment..
url = '/home/specialFunction';
jQuery.get(url, function(data) {
jQuery("#div2display").html(data);
});
in the case of the above you notice despite it not being a great example that. you have the url with 2 parameters home and specialFunction
home in this case is the controller file for home in the control folder for the home file in views the specialFunction is a "public function" within the class that makes the home controller file. similar to that of index() but a separate function all together. Best way I have found to handle it is through .post() and a callback output expected in JSON cause you can form an array of data on the php side json_encode it and echo out that json_encode and then work with that like you would any JSON output. or if your just expecting a sinlge output and not multiples echoing it out is fine but enough of the end run output thats for you to decide with what your comfortable doing currently. Hopefully all around though this gives you some clairity and hopefully it works out for you.

How do you configure Codeigniter routing to ignore a specific segment string in the url

I'm not sure I'm going down the right path with this - and I am pretty new to routing, so if this is way off base, I apologize.
What I'm trying to accomplish is having a link at the bottom of the page where the site visitor can select mobile or full screen. I want them to be able to do this from any page. When they select either one, there could be any number of things happen - depending on where they are on the site (i.e. different views, functionality, etc).
Right now, I don't have Codeigniter configured to allow querystrings, which is the default behavior. So in order to pass a preference in a link, I need to do it using a url segment.
I was hoping to do something like:
<? if ($in_mobile_view): ?>
Mobile | Full Site
<? else: ?>
Mobile | Full Site
<? endif ?>
This works great when I am navigating to: /welcome/index/m or /calendar/view/m, etc. However, if I'm just at: /welcome/m or /m - where the index method of the controller should kick in, I get a 404 because it can't find the method - since it doesn't exist.
My thought was that via routing, I could configure Codeigniter to ignore the "m" and "f" strings and just operate as if they aren't in the url at all.
Is that a good way to go about this? If not, I'd love to hear other suggestions. If this is a decent way to go, I'd really appreciate if someone could point me in the right direction for routing.
Thanks for your time.
Why select this with the URL?
You can detect basic "is mobile" with the User Agent library. Take that value and set the session to whichever the User Agent suggests, then simply link to a controller that switches the user between mobile and full versions when clicked.
Run this in global code somewhere as a hook or a Base Controller.
if($this->user_agent->is_mobile() && ! $this->session->userdata('site_mode'))
{
$this->session->set_userdata('site_mode', $this->user_agent->is_mobile() ? 'mobile' : 'full');
}
Then you controller can just set the session to whatever based on what they have clicked.
/switch/mobile
/switch/full

Resources