In Laravel how to add language part in all anchor links? - laravel

I am using blade template and put simple url like
About us
Now I change language like
mydomain.com/hn/
then how i can add language part in all url like
mydomain.com/hn/aboutus

Take a look at Laravel localization. It may be useful to you.
Laravel Localization

Try to create a link like this
About us
and then in routes/web.php make a route for it like this
Route::get('aboutus/hn', function () {
return 'About us page';
});

Related

Unable to navigate page with inertia-link if route is dynamic

I cant find a way to navigate to a new page when inertia-link is clicked when using a dynamic route in laravel.
route:
This two is not added together in the code. I tried both way to tested so i put both here
Route::get('/{clothesCategory}', 'ClothesController#getCalsas'); // this doesnt work
Route::get('/NotDynamic, 'ClothesController#getCalsas'); // this work
Html:
<inertia-link href="/NotDynamic">Not Working</inertia-link> //this only works with NotDynamic route
Working // this works with both routes
Why is this happening I dont understand the reason behind it? also how can I get the inertia-link to work with the Dynamic route?
You can simply follow this example:
<inertia-link :href="'/' + data.slug">Go To</inertia-link>
use the ":" (colon symbol) before "href". Then you can insert dynamic value into the link.
Found how to do this using :headers
<inertia-link href="/endpoint" :headers="{ clothesCategory: endpoint}">end point</inertia-link>

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

File extension for Laravel Blade as an 'index' page

My 'homepage' for a Laravel install works great, which is: welcome.blade.php however, my question is - how do I get other basic 'static' pages to work correctly.
My preferred site top level navigation has for example, 'about us', so what I'd like to do is create a directory called 'about us' and then place a similar page as blade.index.php in there but it throws an error.
So, my question is - how do I name the file within a directory within the web application.
Thanks
You've got it flipped. blade always goes in the middle.
index.blade.php
return view('index');
/about/index.blade.php
return view('about.index');
You will need put your view file inside static folder of your choice and make sure your filename should contain blade in the middle for ex: "about.blade.php" and after that you will need to return that file with folder_name/file_name.
return view('Folder_name/File_name');
Try above code you are good to go.

laravel send variable to login route

I am using a different app.blade.php than the one provided with the default auth system in laravel. In the one I have created, I need to give a variable each time so that it show where we are in the code. For example, for my routes, I use:
Route::get('/', ['as' => 'home', function () {
$position = ['main_title'=>'Home','second_title'=>'',
'urls'=>
[
['name'=>'home','url'=>'#']
]
];
return view('home')->with('position',$position);
}]);
And in my app.blade.php, I can use this $position to print the main title etc.
Now the problem comes when I try to access login because $position is not set. How can I achieve that in Laravel so that the auth system will use my new blade system?
Thanks.
The auth views use the layout file app.blade.php located in resources/views/layouts. This is done by the code #extends('layouts.app') at top of all the views, which means that the auth views extend this layout.
To change the layout in all the auth views, manually replace the #extends('layouts.app') part at top with the new layout file you have created. For example if your new custom layout file is resources/views/base.blade.php, then replace the default extends to #extends('base').
If you want to use a variable in different (all) views, you may use ViewComposer

CodeIgniter - adds an extra class to my URL when i click on a link

I'm new in CI as well in php. I have a problem that's bugging me for two days now:
when i click on a link in my admin header(say: Articles) it takes me to: www.example.com/admin/articles, which is ok. If now i try to click on another link in the header (say: Add articles), the url becomes: www.example.com/admin/admin/add_articles - it adds an extra admin to my url. if i click again on Articles, the url will be: www.example.com/admin/admin/admin/articles, and so on.
Do you have any idea why is this happening?
Thanks
You have 2 choice, the first is that you wrote in every link base_url()
OR
you can use a built in helper:
anchor('route','label','attributes')
in your example:
anchor('admin/add_article','Add an article',array('class' => 'link'))
Then that will create this HTML code:
Add an article
use absolute urls not relative, use $config['base_url'] before every link
Don't use
$config['base_url] . 'controller/action',
use the function:
site_url('controller/action');
Or use the anchor function #András Rátz suggested.

Resources