Passing a url value to view thru routes - php

I have a url this http://localhost/laravel/public/page/sign-up now..
I want to get the value of the url which is "sign-up" in the view..
Its not including the sign-up view.. I'm just new in laravel..
and before I posted here, I have done my research and found nothing relevant
in my master.blade.php I am using this
If anybody could enlighten me what is going on here and what is lacking.. I would really appreciate it
View
#if ($page == 'sign-up')
#include('pages/unregistred/sign-up')
#endif
Routes:
Route::get('page/{action}', function($action){
return View::make('layouts.master')->with('page', $action);
});

You can use Named routes:
Route::get('page/{action}', ['as' => 'sign-up', function($action){
return View::make('layouts.master')->with('page', $action);
}]);
And in the layout use:
#if (Route::currentRouteNamed('sign-up'))
#include('pages/unregistred/sign-up')
#endif
Also you can use Route::is() method for matching:
#if (Request::is('page/sign-up'))
Another way if the prefix of the page is /page/:
Request::segment(2); // return "sign-up"
Other info are in the docs: Request Information
I suggest you to use the route to show the correct view and not include them in the layout using conditions.
You can find a guide to the layout here: Blade templating

Try something like this:
Route:
Route::get('page/{type}', 'PageController#index');
Controller:
class PageController extends BaseController {
public function index($type)
{
return View::make('page.register', array('page' => $type));
}
}

Related

url parameter - laravel

our users access our site with a unique parameter on the url. ie http://example.com/hire-agreement?u=unique_param
I've set up a route to a view -
Route::get('/hire-agreement', function () {
return view('hire-agreement');
});
I have 2 questions.
Do I need to add anything else to the Route to allow the parameter to be read in the view?
How do I read this parameter value in the View? Can I use $_GET["name"]) ?
thanks
Craig.
you don't need anything more in the url section. and to use or get url parameter use laravel request() helper.
$value = request('key');
in view you can print a key like
{{ request('name') }}
complete example for you using request helper
Route::get('/hire-agreement', function () {
$name = request('name'); //put the key in a variable
return view('hire-agreement', compact('name')); //send the variable to the view
});
and then in view you can use the variable as
{{ $name }}
if you don't want to use a variable you can use directly request helper in view
{{ request('name') }}
you can use Request class too.
Route::get('/hire-agreement', function (Request $request) {
$name = $request->name;
return view('hire-agreement', compact('name'));
});
however i would suggest you to use a controller. don't use closure in route file. u can't cache them when needed.
http://example.com/hire-agreement?u=unique_param
in laravel you can access both post and get can be access by Request class instance or request() helper so you can do
with helper request()
Route::get('/hire-agreement', function () {
dd(request('u')) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
with Class Request
or
Route::get('/hire-agreement', function (Request $request) {
dd($request->u)) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
here you can
You better pass the request to a controller and handle it there, it's easier and cleaner that way.however if you want to got straight from route to view, you better use the below method.
put this in your route file
Route::get('/hire-agreement/{param}', function ($param) {
return view('hire-agreement')->with($param);
});
in the view you can access the param like this
<p>{{$param}}</p>
now if user request "/hire-agreement/1234" your $param in the view will contain 1234, Also if you would like to access get parameters in the url you can do it like this
{{Request::input('q')}}

Undefined variable in Laravel 8

I have issue with the undefined variable in Laravel 8, I actually don't understand why this happen because I was trying and playing with the Laravel 8.
I created the same method and same coding but somehow when I tried to run the coding for page about I got the error that said
ErrorException
Undefined variable: about (View: D:\Server\htdocs\app\resources\views\pages\about.blade.php)
Why is this happening ? I don't understand. Because I use the exact same coding for my other pages and it works but when I try to open the about page it suddenly give me the error when other pages are perfectly fine with no error.
PagesController
class PagesController extends Controller
{
public function index()
{
$title = "Welcome to my blog";
// return view ('pages.index', compact('title')); // first method
return view ('pages.index')->with('title',$title); // 2 method
}
public function about()
{
$about = "About Page";
return view ('pages.about')->with('about',$about);
}
public function services()
{
$data = array(
'title' =>'Services' // array
);
return view ('pages.service')-> with($data);
}
}
about.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$about}} </h1>
<p> This is about pages </p>
#endsection
index.blade.php
#extends('layouts.app')
#section('content')
<h1>{{$title}} </h1>
<p> This is tutorial </p>
#endsection
Just to show you the same coding i use for index and about
My route if anyone asking
Route::get('/', [PagesController::class,'index']);
Route::get('/about', [PagesController::class,'about']);
Route::get('/services', [PagesController::class,'services']);
index.blade.php
about.blade.php
public function about ()
{
$ about = "About Page";
return view ('pages.about') -> compact ('about');
}
Replace it as is and try it: your error will be solved.
php artisan route:cache
Clearing the route cache helped me.
My variable was added later and data was output from the cache
for people still suffering from this issue , head over to web.php and make sure you dont have a duplicated route.

Laravel does not return data to the view

My Laravel-5.4 project was working fine. but now it's driving me crazy when trying to return data from controller to the view:
Requested url is :
Route::middleware(['web'])->group(function () {
Route::prefix('service')->group( function () {
Route::get('{service_slug}', 'ServiceController#findBySlug')->name('service.find_by_slug');
});
});
Controller action is as below:
function findBySlug ($slug)
{
return back()->withMessage('test message');
}
Here in the blade view i can not catch $message variable, in fact there is no $message variable in the view.
#php
if (isset($message))
dd($message);
#endphp
How can i fix it?...thanks.
You are redirecting. You are 'flashing' data to the session when you redirect 'with` data like that. You have to get it from the session.
Also there is nothing related to passing something from the controller to a view in your post.
Docs 5.4 - Responses - Redirecting with flashed session data

Why is the controller not redirecting to proper view in Laravel 5.4

In the below snippet, I have an html form in my example page with the form's action attribute set to exampleController URI in my Routes as follows:
htmlPage.blade.php
<form action="/exampleController" method="POST">
{{csrf_field()}}
// rest of form stuff goes here
</form>
#if(isset($result))
<table>
<td>{{ $result }}</td>
</table>
#endif
Web.php
Route::get('/gotoform', 'PageController#showform');
Route::post('/exampleController', 'PageController#processData');
Controller.php
class PageController extends Controller
{
public function showform()
{
return view('htmlPage');
}
}
public function processData(Request $request)
{
$result = $request['htmlInputData'];
return view('htmlPage')->with(compact('result');
}
}
Everything is fine from here and data is being processed and displayed correctly on the page but I was wondering why the URL address the controller returned for the $result variable is mydomain.com/exampleController and not mydomain.com/htmlPage which is the html page responsible for displaying the results of the operation.
Also, since the resulting URL address is mydomain.com/exampleController (which is not what I expected), its returning the following error when manually refreshed:
MethodNotAllowedHttpException in RouteCollection.php line 233:
Can somebody please enlighten me what did I missed?
thanks in advance.
EDIT:
by changing to Route::any the MethodNotAllowedHttpException error has gone away but still the URL returned by the controller is not right.
This happens because you return a View (view('htmlPage')).
if you want to access this url mydomain.com/htmlPage you need to redirect not load a View.
Use this return redirect(url('/htmlPage'))->with(compact('result');
In order to load this successfully you need to have another route :
Route::get('/htmlPage', 'PageController#loadhtmlPage');
and also to load the view in this url you have to do this in your controller:
public function loadhtmlPage(Request $request)
{
return view('htmlPage');
}

Laravel 4 - Handling 404 Errors

I know that to handle 404 errors with laravel 4 is to write at app/start/global.php :
App::missing(function($exception)
{
return Redirect::route('404_Error');
});
But actually I want to use this route:
Route::get('error', array(
'as' => '404_Error',
'uses' => 'ErrorController#get404Error'
));
But to stay at the same URL.
Example:
My URL right now is localhost:8000/users/blat (404 Error). I don't want redirect to error page. I want to see ErrorController#get404Error at this URL.
Thanks so much.
You may try something like this:
App::missing(function($exception)
{
return App::make('ErrorController')->get404Error($exception);
});
Your ErrorController:
class ErrorController extends BaseController {
//...
public function get404Error($exception)
{
//...
$data = ...;
return View::make('error_view')->with('data', $data);
}
}
If you use a different route it will redirect to that route, you probably will need to render your view right away, IMO it's the only way to stay where you are and not have your url changed:
App::missing(function($exception)
{
return View::make('404_Error');
});
You can just create an instance of the ErrorController class and call its get404Error method. That way you will have the variables from your BaseController class too and the route will stay the same.
App::missing(function($exception)
{
$errorController = new ErrorController();
return $errorController->get404Error();
});

Categories